views:

768

answers:

4

What is difference between mysql,mysqli and pdo ?
Which one is the best suited to use with PHP-MYSQL?

+5  A: 

Those are differents API to access a MySQL backend

  • the mysql is the historical API
  • the mysqli is a new version of the historical API, it should perform better and have a better set of function, also the API is object oriented
  • PDO_MySQL, is the MySQL for PDO, PDO has been introduced in PHP, the project aims to make a common API for all the databases access, so in theory you should be able to migrate between RDMS without changing any code(if you don't use specific RDBM function in your queries), also object oriented

So it depend what kind of code your want to produce, if you prefer object oriented layers or plain functions...

My advice would be

  1. PDO
  2. mysqli
  3. mysql

Also my feeling, the mysql API would probably being deleted in future releases of PHP

RageZ
+2  A: 

mysqli is the enhanced version of mysql.

PDO extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions.

Sarfraz
+11  A: 

There are (more than) three popular ways to use MySQL from PHP.

  1. The mysql functions are procedural and use manual escaping.
  2. mysqli is a replacement for the mysql functions, with object-oriented and procedural versions. It has support for prepared statements.
  3. PDO (PHP Data Objects) is a general database abstraction layer with support for MySQL among many other databases. It provides prepared statements, and significant flexibility in how data is returned.

I would recommend using PDO with prepared statements. It is a well-designed API and will let you more easily move to another database (including any that supports ODBC) if necessary.

Matthew Flaschen
There is often the confusion that for procedural you have to use mysql_*() functions where for OO you use mysqli!The fact is that mysqli completely replace the early (PHP 4) implementation of mysql. It is possible to use both the procedural style and the OO one with mysqli.
Patrick Allaert
@Patrick, thanks. I've corrected this.
Matthew Flaschen
A: 

There is a table comparing the 3 API features. Use Mysqli whenever possible as is the latest launched after PDO and is and will be better maintained in the future.

Elzo Valugi
This is simply wrong. `mysqli` and `PDO` were [both released](http://www.php.net/manual/en/mysqli.overview.php) for PHP 5.0. And you haven't given any basis for suggesting PDO won't be maintained.
Matthew Flaschen
I didn't suggest that. PDO and mysqli have different functions and probably they will both be mantained. PDO MAIN FEATURE is the consistent API over different drivers, mysql being only one of them.
Elzo Valugi