MySQLi vs PDO - Which is the most recommended to use?

57

With mysql_* entering the deprecated state, the PHP documentation recommends using the PDO and MySQLi.

Which do you recommend for use? PDO seems to be more suitable for working with object-oriented only than at the same time I saw benchmarks which show that performance using MySQLi would perform better, could the performance impact a lot if compared to using the PDO?

    
asked by anonymous 06.03.2014 / 18:10

4 answers

42

MySQLi:

Advantages:

  • Object Oriented and procedural API;
  • High performance;
  • Syntax relatively simpler (and similar to old API mysql_* );

Disadvantages:

  • Only works with MySQL databases;
  • Has no named parameters;
  • Does not have client-side prepared statements

PDO:

Advantages:

  • It works with 12 different database drivers (4D, MS SQL Server, Firebird / Interbase, MySQL , Oracle , ODBC / DB2, PostgreSQL , SQLite , Informix, IBM, CUBRID);
  • Object Oriented API;
  • Has named parameters;
  • It has prepared statements on the client side (see disadvantages below)

Disadvantages:

  • Not as fast as MySQLi;
  • By default, it simulates prepared statements (you can enable the native version when configuring its connection to the database, but if the native version does not work for some reason, it returns to simulate prepared statements without triggering errors or warnings . More details here )

Between the two options I give preference to the PDO, even though it is a bit slower (between 2% and 7%). As I see it, the fact that the PDO communicates with more DB drivers and having prepared statements , which is of great value when it comes to security, in my opinion makes this technology more interesting. p>     

06.03.2014 / 20:08
32

If you're concerned with performance and have no interest in portability, mysqli is the best choice.

One of the differences is that with mysqli the prepared queries are implemented on the server side of the database, while with the PDO they are emulated on the client side.

This means that every time you run a PHP script, with the PDO the prepared queries have to be compiled again to generate an unprepared common query, and PHP is spending time recompiling the query and sending the whole query to the query. MySQL server.

With mysqli, PHP only sends the parameters every time the same prepared query is sent to the server when it uses the same persistent connection, even though it is servicing a different HTTP request.

The possible advantage of PDO portability often turns out to be utopian because most applications when they opt for mysql, end up not changing again.

    
07.03.2014 / 12:33
13

It will depend on your project, the difference in the PDO is not as great as it stands, as an example, Drupal that is a CMS master uses PDO, and how does eclipse use drupal in its market place .

My recommendation: Use the PDO. Unless your application is vitally dependent on speed, in that case use MySQL

    
06.03.2014 / 18:17
13

The great advantage of PDO compared to mysqli is that it supports multiple databases and allows the use of parameterized names in prepared queries while mysqli only supports MySQL > and in query preparads only ? is used. In the manual there is a comparison of the PDO, mysqli and mysql features in this link .

Follow other links that make the comparison:

PDO vs. Mysqli which should you use net

With is better Mysqli or PDO in PHP

    
06.03.2014 / 18:17