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.
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 |
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
Related Links
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.
PDO Installation
For installation details, go to: http://php.net/manual/en/pdo.installation.php.
Related Links