Tuesday, December 13, 2011

Text encoding transformations

In PHP there are several text encoding transformation that are useful for different purposes.
If you use the GET method, you can make it safer if you pass you Get variable using
  • rawurlencode: Use if you have spaces in the main part of the URL. Replaces non alpha-numeric characters with %ASCII code
  • urlencode: Used in the query part of a URL, or to pass GET variables. Replaces spaces with +
  • htmlspecialcharacters:  Use to prevent user-supplied text from containing HTML markup. Does minimal conversion of HTML characters:  " < > & ' to &XXXX; equivalent
  • htmlentities: Used to prevent user-supplied text from adding any malicious code. Converts all characters with an &...; equivalent.
  • strip_tags: Use to prevent user-supplied text from containing unallowed HTML markup.
  • nl2br: Use to preserve line breaks in user-supplied text.
Example:  Passing on a query
'<a href= "http://www.google.com/?q="'.urlencode("the database student").' ">' ; yields
<a href="https://www.google.com/?q=the+database+student " >
Example: Writing trusted user text to a content page with limited HTML markup and pagebreaks preserved
<?php echo strip_tags(nl2br($sel_page['content']),"<b><br><li><ol><ul><a><i><strong><emp>" ); ?>
 

Friday, October 14, 2011

Connecting Oracle SQL Developer to MySQL

Scenario

You have an Oracle Database and a MySQL database running on your Windows 7 machine that you are using for training/development purposes. You use Oracle SQL Developer to run code on the local Oracle database. Now you want to use Oracle SQL Developer to run code on your local MySQL Database.

Solution Summary

You need to unzip the JDBC (Java DataBase Connecttion) Driver for MySQL to somewhere on your harddrive, and link to it in Oracle SQL Developer. You will then see a MySQL tab in the New/Select Database Connection dialog which will allow you to connect to your SQL databases.

Procedure

  1. Download the JDBC Driver from the MySQL website. I found it at dev.mysql.com/downloads/connector/j/
    • Note: You should have a MySQL account to down load this. The proper file will have a name like mysql-connector-java-5.1.18.zip (the .tar.gz version, also known as a "tar ball" is for Unix/Linux operating systems)
  2. To install it all you need to do is unzip the file and put the whole folder in an appropriate place. (The instructions included with the driver say that say you need to set a CLASSPATH environment variable. I did not set this and it worked for me.) Here are some places to put it:
    • In your \sqldeveloper\jdbc\ folder
    • In your <ORACLE_HOME>\jdbc\ folder
  3. Open Oracle SQL Developer.
  4. Click Tools|Preferences|Database|Third Party JDBC Drivers
  5. Click Add Entry
  6. Navigate to and open the directory where you put the unzipped JDBC Driver then Select   the .jar file with the same name as the directory. (mysql-connector-java-5.1.18.jar)
  7. Click OK.
  8. Make sure your MySQL database is running.
  9. Use the Connections Tab make a new connection.
Result: You should see a MySQL tab in the New/Select Database window.

Test it out

  1. Create a connection name: root_mysql
  2. Username: root
  3. Password: <If you have secured your database with a password type it here>
  4. Hostname: probably localhost (or 127.0.0.1 or your computer's name)
  5. Port: probably the default for MySQL 3306.
  6. Choose Database: If you have put the proper information in above, this will show you a dropdown list of the databases in your MySQL configuration. Choose mysql.
  7. Click Test.

Result: Hopefully you will see a Status: Success message, and a new connection will appear in your connections pane.