== Why use PDO? ==
 
== 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 <code>mysql_real_escape_string</code> 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!).
+
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...  and it's method for escaping parameters with <code>mysql_real_escape_string</code> 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 the 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 an easier and 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 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 an easier and 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 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.
+
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 ==
 
== Connecting to MySQL ==
 
</source>
 
</source>
   −
You can also pass in several driver options as an array to the fourth parameters. I recommend passing the parameter 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, really only usable if you are using an old version of MySQL.
+
You can also pass in several driver options as an array to the fourth parameters. I recommend passing the parameter which puts PDO into exception mode, which I will explain in the next section. The other parameter is to turn off prepare emulation which is enabled in MySQL driver by default, but really should be turned off to use PDO safely and is really only usable if you are using an old version of MySQL.
 
<source>
 
<source>
 
<?php
 
<?php
 
</source>
 
</source>
   −
You can also set some attributes after PDO construction with setAttribute method:
+
You can also set some attributes after PDO construction with the <tt>setAttribute</tt> method:
 
<source>
 
<source>
 
<?php
 
<?php
 
</source>
 
</source>
   Exception encountered, of type "Error"
[067b8013] /index.php?title=PDO_Tutorial_for_MySQL_Developers&diff=prev&oldid=632 Error from line 434 of /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DairikiDiff.php(544): DiffEngine->diag(integer, integer, integer, integer, integer)
#1 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq(integer, integer, integer, integer)
#2 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal(array, array)
#3 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DairikiDiff.php(721): DiffEngine->diff(array, array)
#4 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DairikiDiff.php(859): Diff->__construct(array, array)
#5 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DairikiDiff.php(980): MappedDiff->__construct(array, array, array, array)
#6 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/TableDiffFormatter.php(194): WordLevelDiff->__construct(array, array)
#7 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DiffFormatter.php(140): TableDiffFormatter->changed(array, array)
#8 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DiffFormatter.php(82): DiffFormatter->block(integer, integer, integer, integer, array)
#9 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DifferenceEngine.php(888): DiffFormatter->format(Diff)
#10 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DifferenceEngine.php(802): DifferenceEngine->generateTextDiffBody(string, string)
#11 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DifferenceEngine.php(733): DifferenceEngine->generateContentDiffBody(WikitextContent, WikitextContent)
#12 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DifferenceEngine.php(662): DifferenceEngine->getDiffBody()
#13 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DifferenceEngine.php(632): DifferenceEngine->getDiff(string, string, string)
#14 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/diff/DifferenceEngine.php(453): DifferenceEngine->showDiff(string, string, string)
#15 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/page/Article.php(795): DifferenceEngine->showDiffPage(boolean)
#16 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/page/Article.php(506): Article->showDiffPage()
#17 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/actions/ViewAction.php(44): Article->view()
#18 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/MediaWiki.php(395): ViewAction->show()
#19 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/MediaWiki.php(273): MediaWiki->performAction(Article, Title)
#20 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/MediaWiki.php(566): MediaWiki->performRequest()
#21 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/includes/MediaWiki.php(414): MediaWiki->main()
#22 /misc/21/000/105/835/1/user/web/wiki.hashphp.org/index.php(41): MediaWiki->run()
#23 {main}