Difference between revisions of "PDO Tutorial for MySQL Developers"

From Hashphp.org
Jump to: navigation, search
(Created page with "==Why use PDO?== mysql_* functions are getting old. For a long time now mysql_* has been at odds with other common SQL database programming interfaces. It doesn't support moder...")
 
Line 4: Line 4:
  
 
PDO has a much nicer interface, you will end up being more productive, and write safer and cleaner code.  PDO also has different drivers for different SQL database vendors which will allow you to easily use other vendors without having to relearn a different interface. (though you will have to learn slightly different SQL probably).  Instead of concatenating escaped strings into SQL, in PDO you bind parameters which is easier cleaner way of securing queries.  Binding parameters also allow for a performance increase when calling the same SQL query many times with slightly different parameters.  PDO also has multiple methods of error handling.  The biggest issue I have seen with much mysql_* code is that it lacks consistent handling, or no handling at all!  With PDO in exception mode, you can get consistent error handling which will end up saving you loads of time tracking down issues.
 
PDO has a much nicer interface, you will end up being more productive, and write safer and cleaner code.  PDO also has different drivers for different SQL database vendors which will allow you to easily use other vendors without having to relearn a different interface. (though you will have to learn slightly different SQL probably).  Instead of concatenating escaped strings into SQL, in PDO you bind parameters which is easier cleaner way of securing queries.  Binding parameters also allow for a performance increase when calling the same SQL query many times with slightly different parameters.  PDO also has multiple methods of error handling.  The biggest issue I have seen with much mysql_* code is that it lacks consistent handling, or no handling at all!  With PDO in exception mode, you can get consistent error handling which will end up saving you loads of time tracking down issues.
 +
 +
PDO is enabled by default in PHP installations now, however you need two extensions to be able to use PDO: PDO, and a driver for the database you want to use like pdo_mysql.  installing the MySQL driver is as simple as installing the php-mysql package in most distributions.
 +
 +
==Connecting to MySQL==
 +
 +
old way:
 +
<source>
 +
<?php
 +
$link = mysql_connect('localhost', 'user', 'pass');
 +
mysql_select_db('testdb', $link);
 +
mysql_set_charset('UTF-8', $link);
 +
</source>
 +
 +
new way: all you gotta do is create a new PDO object.  PDO's constructor takes at most 4 parameters, DSN, username, password, and an array of driver options.
 +
 +
A DSN is basically a string of options that tell PDO which driver to use, and the connection details... You can look up all the options here [http://www.php.net/manual/en/ref.pdo-mysql.connection.php PDO MYSQL DSN].
 +
<source>
 +
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8', 'username', 'password');
 +
</source>
 +
 +
You can also pass in several driver options as an array to the fourth parameters. I recommend passing two parameters, One which puts PDO into exception mode, which I will explain in the next section. The other is to turn off prepare emulation which is enabled in MySQL driver by default, but really should be turned off to use PDO safely.
 +
<source>
 +
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=UTF-8', 'username', 'password', array(PDO::ATTR_EMULATE_PREPARES => false,
 +
                                                                                                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
 +
</source>

Revision as of 12:58, 30 July 2011

Why use PDO?

mysql_* functions are getting old. For a long time now mysql_* has been at odds with other common SQL database programming interfaces. It doesn't support modern SQL database concepts such as prepared statements, stored procs, transactions etc... It's method for escaping parameters with mysql_real_escape_string and concatenating into SQL strings is error prone and old fashioned. The other issue with mysql_* is that it has had a lack of attention lately from developers, it is not being maintained... Which could mean things like security vulnerabilities are not getting fixed, or it may stop working altogether with newer versions of MySQL. Also lately PHP community have seen fit to start a soft deprecation of mysql_* which means you will start seeing a slow process of eventually removing mysql_* functions altogether from the language (Don't worry this will probably be awhile before it actually happens!).

PDO has a much nicer interface, you will end up being more productive, and write safer and cleaner code. PDO also has different drivers for different SQL database vendors which will allow you to easily use other vendors without having to relearn a different interface. (though you will have to learn slightly different SQL probably). Instead of concatenating escaped strings into SQL, in PDO you bind parameters which is easier cleaner way of securing queries. Binding parameters also allow for a performance increase when calling the same SQL query many times with slightly different parameters. PDO also has multiple methods of error handling. The biggest issue I have seen with much mysql_* code is that it lacks consistent handling, or no handling at all! With PDO in exception mode, you can get consistent error handling which will end up saving you loads of time tracking down issues.

PDO is enabled by default in PHP installations now, however you need two extensions to be able to use PDO: PDO, and a driver for the database you want to use like pdo_mysql. installing the MySQL driver is as simple as installing the php-mysql package in most distributions.

Connecting to MySQL

old way:

 

new way: all you gotta do is create a new PDO object. PDO's constructor takes at most 4 parameters, DSN, username, password, and an array of driver options.

A DSN is basically a string of options that tell PDO which driver to use, and the connection details... You can look up all the options here PDO MYSQL DSN.

 

You can also pass in several driver options as an array to the fourth parameters. I recommend passing two parameters, One which puts PDO into exception mode, which I will explain in the next section. The other is to turn off prepare emulation which is enabled in MySQL driver by default, but really should be turned off to use PDO safely.