How to Use PHP Improved MySQLi extension (and Why You Should)

PHP supports MySQL using a PHP extension. Thousands or millions projects have been written worldwide using PHP and MySQL. PHP team announced plans to deprecate MySQL extension in mid 2011. Old MySQL extension officially deprecated since PHP 5.5.0 in late 2012 and it will be removed in the future. The alternatives since PHP 5 and later are MySQLi (“i” stands from “improved”) and PDO (PHP Data Objects).

Everyone can understand that is not so easy to immediately migrate old projects. However, the old extension must not be used anymore in new development.

Old extension didn’t support Prepared Statements. Both MySQLi and PDO are object oriented and do support Prepared Statements (also support Transactions, Stored Procedures and more). Prepared Statements are very important for web application security, as they protect from SQL injection. Using Prepared Statements you do not have to escape strings before insert them in Database. Moreover, PDO offers support for many databases (not only MySQL).

So, the question is “Which Should I Use? MySQLi or PDO?”. A short answer would be “whatever you like”. Personally, I prefer MySQLi. I could select PDO if “multiple database support” was included in project requirements (however, in this case php ADODB could also be an alternative).

Below I describe the common use of MySQLi in php development with MySQL database (it can be also used with MariaDB, an enhanced, drop-in replacement for MySQL).

How to Use PHP Improved MySQLi extension (and Why You Should).