Documentation Home
Connectors and APIs Manual
Download this Manual

7.2.3 Choosing an API

Copyright 1997-2021 the PHP Documentation Group.

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

Example 7.1 Comparing the three MySQL APIs

// mysqli
$mysqli = new mysqli("", "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(';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']);
// mysql
$c = mysql_connect("", "user", "password");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);

Recommended API

It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7. A detailed feature comparison matrix is provided below. The overall performance of all three 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%.

Feature comparison

PHP version introduced5.05.12.0
Included with PHP 5.xYesYesYes
Included with PHP 7.xYesYesNo
Development statusActiveActiveMaintenance only in 5.x; removed in 7.x
LifecycleActiveActiveDeprecated in 5.x; removed in 7.x
Recommended for new projectsYesYesNo
OOP InterfaceYesYesNo
Procedural InterfaceYesNoYes
API supports non-blocking, asynchronous queries with mysqlndYesNoNo
Persistent ConnectionsYesYesYes
API supports CharsetsYesYesYes
API supports server-side Prepared StatementsYesYesNo
API supports client-side Prepared StatementsNoYesNo
API supports Stored ProceduresYesYesNo
API supports Multiple StatementsYesMostNo
API supports TransactionsYesYesNo
Transactions can be controlled with SQLYesYesYes
Supports all MySQL 5.1+ functionalityYesMostNo