PHP MySQL Extension - MySqli and PDO


PHP 5 and later can work with a MySQL database using :

  • MySQLi extension (the "i" stands for improved)
  • PDO (PHP Data Objects)

Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012.


Should I Use MySQLi or PDO

If you need a quick response, "Whatever you want" will suffice.

Both MySQLi and PDO have their advantages :

MySQLi will only function with MySQL databases, but PDO will operate with 12 different database systems.

MySQLi will supports all features of mysql databases where PDO will not supports all features of mysql databases.

So, PDO simplifies the procedure, if your project has to be switched to another database. You just need to update the link string and a couple of questions. You will require the whole code rewriting with MySQLi, which includes queries.

Although they are both object-oriented, MySQLi also has a procedural API.

Prepared Statements are supported by both. Prepared Statements guard against SQL injection and are critical for online application security.

You can also search for these topics, php Should I Use MySQLi or PDO, php use mysqli or pdo, php different database systems, php mysqli switched to another database, php mysqli advantages, php pdo advantages, php mysqli procedural api.

PHP MySqli vs PDO

Now we can see a few differences between Mysqli and PDO in the below table.

Features MySqli PDO
DB Support Only MySql (All Version) 12 different databases
API OOP + Procedural OOP Only
Recommended for new MySql Projects Yes - preferred option Yes
API supports Multiple Statements Yes No
Supports all MySQL 4.1+ functionality Yes No
You can also search for these topics, php mysqli vs pdo, difference between mysqli vs pdo in php, pdo vs mysqli performance and security , comparision of php pdo and mysqli.

MySQL Examples in Both MySQLi and PDO Syntax

We'll show you three ways to work with PHP and MySQL in this and the subsequent chapters :

  • MySQLi (object-oriented)
  • MySQLi (procedural)
  • PDO
You can also search for these topics, php mysqli syntax, syntax to perform php mysql pdo, Explain three ways of working with php and mysql, php mysql Example in both mysqli and pdo syntax.

MySQLi Installation

When the php5 mysql package is installed, the MySQLi extension is usually installed automatically on Linux and Windows.

For installation details, go to: http://php.net/manual/en/mysqli.installation.php.

You can also search for these topics, php mysqli installation, php mysqli working operating systems, how to install php mysqli, installing the php mysqli.

PDO Installation

For installation details, go to: http://php.net/manual/en/pdo.installation.php.

You can also search for these topics, php mysqli installation, how to install php mysqli, installing the php mysqli.