HTTP/1.1 200 OK Connection: close Date: Sun, 02 Apr 2017 08:06:28 GMT Server: nginx/1.0.12 Content-Language: en Content-Type: text/html; charset=utf-8 Last-Modified: Sun, 02 Apr 2017 04:05:48 GMT Client-Date: Sun, 02 Apr 2017 06:06:31 GMT Client-Peer: 193.19.243.212:80 Client-Response-Num: 1 Client-Transfer-Encoding: chunked Content-Base: http://ua2.php.net/manual/en/mysqlinfo.api.choosing.php Link: ; rel=shorturl Set-Cookie: COUNTRY=NA%2C195.128.17.206; expires=Sun, 09-Apr-2017 08:06:28 GMT; path=/; domain=.php.net Status: 200 OK Title: PHP: Choosing an API - Manual X-Frame-Options: SAMEORIGIN X-Meta-Charset: utf-8 X-Meta-Viewport: width=device-width, initial-scale=1.0 X-Powered-By: PHP/5.3.10
This version of the page http://www.php.net/mysqlinfo.api.choosing.php (0.0.0.0) stored by archive.org.ua. It represents a snapshot of the page as of 2017-04-02. The original page over time could change.
PHP: Choosing an API - Manual
PNWPHP 2017 CfP
Edit Report a Bug

Choosing an API

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 "example.com" using the username "user" and the password "password". And a query is run to greet the user.

Example #1 Comparing the three 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']);

// mysql
$c mysql_connect("example.com""user""password");
mysql_select_db("database");
$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

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

User Contributed Notes 4 notes

up
down
28
alvaro at demogracia dot com
4 years ago
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.
up
down
1
Anonymous
3 months ago
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.
up
down
1
michaeln at associations plus dot see eh
4 years ago
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.
up
down
-90
Anonymous
1 year ago
My suggestion will be to use a library that hides the internals of the specific extension.  For example, now that in php 5.5 mysql is deprecated, if you were using  PHP Adodb, all you had to do is

go from : $cn = NewADOConnection('mysql') to $cn = NewADOConnection('mysqli');

This is a huge benefit. Also if you are changing your database from mysql to sql or oracle, is just changing one parameter.  I wish someone told me this when I started.
add a note