tags:

views:

2232

answers:

7

Which one should be used php PDO or normal mysql_connect to execute database query through 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 then mysql_connect.

So should i use PDO over those reasons or stick to the traditional mysql_connect

+4  A: 

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.

gnud
I think you meant negligible
MarkR
Interesting... But every mysql_cnnect has been closed by using mysql_close; though not a single PDO has been closed or unset... May be we should close the PDO too or do not close the mysql_connect; only then we may get more accurate result. You need some extra time to close the connection.
Sadi
The PDO object destructor is called (and disconnects) when $db is set to null. Have a look at the PDO documentation.
gnud
http://us2.php.net/manual/en/pdo.connections.php I am not sure if it is removed from memory at the moment you declare new connection for the same variable.
Sadi
just to let you know that your test ain't fair because the raw test selects a database while the PDO test doesn't.
thephpdeveloper
+1 @thephpdeveloper Thank you for pint at the fairness :) All benchmark should use same procedure to show the real comparison. Even if PDO close the connection after $db=null, it supposed to be separate case.
Sadi
+2  A: 

I don't think speed is what people are looking for when they are using PDO -- I don't know if there is a difference, and I honnestly don't care : as long as I'm doing a couple of queries to a database when generating a page, a couple of milliseconds on the PHP side will not change anything.

There are two/three great things with PDO, compared to mysql_* :

  • More or less constant interface accross databases ; better than using mysql_*, pg_*, oci_*, ...
  • Object-Oriented API (mysqli_* has an OO-API, but not mysql_*)
  • Support new features of MySQL >= 4.1 (same as mysqli_*, but not mysql_*, again)

BTW : I'm generally using PDO -- either "by hand", or as it's integrated in / used by Zend Framework and/or Doctrine.


As a sidenote : Even if you are not going to use PDO, note that using mysqli instead of mysql is recommended.

See this page of the PHP manual, about that.

Pascal MARTIN
PDO doesn't allow us to use all of the advanced features that are available in the latest versions of MySQL server. For example, PDO does not allow us to use MySQL's support for Multiple Statements.So then, Should I use PDO?
Imrul
But whenever you need portability or your project/site is big enough, you should use PDO for better maintenance, cleaner code. Though it is possible to make clean code using mysql_*, you may have hard time to change driver etc.
Sadi
A: 

If performance isn't a "real problem" for you, you should use PDO. The performance differs by small margins, and PDO has a very nice and portable cross-database interface wich can save you some headaches in case you need to use several database drivers.

yoda
+8  A: 

PDO is a bit slower than the mysql_* But it has great portability. PDO provides single interface across multiple databases. That means you can use multiple DB without using mysql_query for mysql, mssql_query for MS sql etc. Just use something like $db->query("INSERT INTO...") always. No matter what DB driver you are using.

So, for larger or portable project PDO is preferable. Even zend framework use PDO :)

Sadi
Zend Framework can also use mysqli ...
johannes
A: 

I would generally recommend using PDO unless there is a specific reason you cannot. If there is no little difference between the two and you have no reason not to use PDO, I believe it would be better to get into the practice of using DB abstraction in your applications than going with mysql_* simply because it is there. I would say let best practice win.

Robert DeBoer
A: 
  • 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
  • buntch of useful methods (like fetch* family)