Alex Rivera | Logout

PHP PDO vs normal mysql_connect

Asked 2009-09-09T20:52:06.477
47

Should I use php PDO or normal mysql_connect to execute database queries in PHP?

Which one is faster?

One of the big benefits of PDO is that the interface is consistent across multiple databases. There are some cool functions for prepared statements too, which take some of the hassle out of escaping all your query strings. The portability of PDO is greater than mysql_connect.

So, should I use PDO for those reasons or stick to the traditional mysql_connect?

Edit
Report

3 Answers

18

Some quick timings indicate PDO is slightly faster at connecting.

$start = microtime(true);
for($i=0; $i<10000; ++$i) {

    try {
        $db = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage()."\n";
    }
    $db = null;
}

$pdotime = microtime(true) - $start;
echo "PDO time: ".$pdotime."\n";

$start = microtime(true);
for($i=0; $i<10000; ++$i) {
    $db = mysql_connect($host, $user, $password);
    if(!$db) {
        echo "Connection failed\n";
    }
    if(!mysql_select_db($schema, $db)) {
        echo "Error: ".mysql_error()."\n";
    }
    mysql_close($db);
}

$rawtime = microtime(true) - $start;
echo "Raw time: ".$rawtime."\n";

Gives results like

PDO time: 0.77983117103577
Raw time: 0.8918719291687

PDO time: 0.7866849899292
Raw time: 0.8954758644104

PDO time: 0.77420806884766
Raw time: 0.90708494186401

PDO time: 0.77484893798828
Raw time: 0.90069103240967

The speed difference will be negligible anyway; establishing a network connection will likely take a LOT longer than any overhead incurred by PDO, especially if the mysql server is on another host.

You mentioned all the reasons to use PDO yourself. Really, never use the mysql_* functions directly, either use PDO, or use some other library.

answered 2009-09-09T20:55:20.780
11
  • With PDO you can uses binded params and that will prevent most sql injection attacks.
  • You can gain more speed using PDO prepared statements.
  • standard interface to all db backends
  • There are a bunch of useful methods (like the fetch* family)
answered 2009-09-11T20:15:36.033
6

I did some performance testing to compare Mysqli functions to PDO functions using both prepared statements and regular direct queries (tested using select statements on Mysqlnd and MyISAM tables).

I found that PDO queries are just slightly slower than Mysqli, but only slightly. This makes sense since PDO used for this purpose mostly just a wrapper that calls Mysqli functions. The advantage to using PDO is that it makes it a little easier to migrate to a different database because the function names aren't specific to MySQL.

The real performance difference is in whether you use prepared queries. There is a large and significant performance penalty to using prepared queries. Other people who have tested them have found the same results.

The only time prepared queries are faster is if you are preparing a query once and then submitting it thousands of times with different data values. Otherwise, it's always faster to use mysqli::query() or PDO::query(). But it's important to be aware that those functions don't escape data values for you, so you need to remember to use mysqli::real_ escape_ string() or PDO::quote() on data variables.

answered 2012-04-30T01:39:43.917

Your Answer