Choosing an API

PHP offers different APIs to connect to MySQL. Below we show the APIs provided by the mysqli and PDO extensions. Each code snippet creates a connection to a MySQL server running on "example.com" using the username "user" and the password "password". And a query is run to greet the user.

Example #1 Comparing the MySQL APIs

<?php
// mysqli
$mysqli = new mysqli("example.com""user""password""database");
$result $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row $result->fetch_assoc();
echo 
htmlentities($row['_message']);

// PDO
$pdo = new PDO('mysql:host=example.com;dbname=database''user''password');
$statement $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row $statement->fetch(PDO::FETCH_ASSOC);
echo 
htmlentities($row['_message']);

Feature comparison

The overall performance of both extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%.

  ext/mysqli PDO_MySQL
PHP version introduced 5.0 5.1
Included with PHP 7.x and 8.x Yes Yes
Development status Active Active
Lifecycle Active Active
Recommended for new projects Yes Yes
OOP Interface Yes Yes
Procedural Interface Yes No
API supports non-blocking, asynchronous queries with mysqlnd Yes No
Persistent Connections Yes Yes
API supports Charsets Yes Yes
API supports server-side Prepared Statements Yes Yes
API supports client-side Prepared Statements No Yes
API supports Stored Procedures Yes Yes
API supports Multiple Statements Yes Most
API supports Transactions Yes Yes
Transactions can be controlled with SQL Yes Yes
Supports all MySQL 5.1+ functionality Yes Most

User Contributed Notes

rellect at gmail dot com 20-Feb-2019 11:37
Another point to consider and one of the advantages of PDO is that it's not limited to mysql and supports other database engines such as PostgreSQL, SQLite, etc.
Anonymous 29-Dec-2016 11:56
These are quite possibly the most paradoxical lines in this table:

API supports non-blocking, asynchronous queries with mysqlnd
     ext/mysqli: Yes     PDO_MySQL: No
API supports client-side Prepared Statements
     ext/mysqli: No     PDO_MySQL: Yes

Apparently it's either asynchronous I/O or the security of bound parameters.
michaeln at associations plus dot see eh 12-Mar-2013 02:49
Another useful consideration to keep in mind when choosing your library is how extensible it is. Chances are, in any sufficiently advanced development scenario, you're going to be extending your database access class to add a method (or multiple methods) for how to handle database errors and alert the development team of errors and whether to have the code fail immediately or fail gracefully serving the user a user-friendly failure notice.

For example, I have a class where I have added extra parameters to the query() function (and a few others), which accept the __FILE__ and __LINE__ constants to facilitate tracking issues. If this were not reasonably possible with PDO-mysql for example (not sure, never used it), it may make one option or the other much less likely to be viable for your usage case.
alvaro at demogracia dot com 21-Jun-2012 09:29
Apart from the feature list, I suggest you try out both MySQLi and PDO and find out what API design you like most. MySQLi is more powerful and probably more complex to learn. PDO is more elegant and has the advantage that you only need to learn one PHP API if you need to work with different DBMS in the future.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有