3

I want to know when and where we use PDO's other than simple mysql_connect methods?

Is this PDO is faster ? what are the advantages over other methods?

coderex
  • 27,225
  • 45
  • 116
  • 170

3 Answers3

8
  1. Security: The most useful thing with PDO are the prepared statements. With them, writing code which can be SQL Injected is far more complicated. ;-)

  2. Errorhandling: On the other hand the errorhandling is much much better as you can switch to Exceptions instead of lousy FATALs ERRORs and so on.

  3. Extensibility: With the mysql_ methods you're strictly bound to mysql. Maybe sometime in the future you want to use antoher database and have to rewrite all your connection and query issuing code. If you're using MySQL specific queries this point has no value to you as you have to rewrite most of your database code anyway.

Malax
  • 9,436
  • 9
  • 48
  • 64
  • 1
    Exceptions are nice yes.. just remember that PDOException is not a subclass of Exception, so a catch (Exception $E) will not catch it. – Anti Veeranna Aug 31 '09 at 17:20
  • 1
    @Anti: I realize your comment is a bit dated, which is why I ask: is this still accurate? From the info I can find, PDOException extends RuntimeException, which extends Exception. – Michelle Tilley Sep 13 '10 at 15:43
  • 3
    Not anymore no, I tried with 5.3.3, PDOException does extend Exception and catch Exception works as expected. – Anti Veeranna Sep 13 '10 at 16:31
3

The PDO introduction in the PHP documentation details much of this basic information. To some extent, PDO is to PHP as JDBC is to Java. Except, well, not quite as nice.

Rob
  • 47,999
  • 5
  • 74
  • 91
0

Apart from the portability, mysqli provides you with pretty much the same as PDO does (like prepared statements, transactions and so on) except that mysqli is a bit faster than PDO.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500