PHP offers you more than one way to connect and interact with your MySQL database. Two common ways to interact with MySQL through your PHP script are the mysql and the mysqli extensions. Both accomplish similar things but there are distinct and notable differences between the two.
PHP’s MySQL Extension
PHP’s mysql extension is the original extension that was designed to allow you to develop PHP applications that interact with your MySQL database. The mysql extension was introduced prior to PHP version 3 and provides a procedural interface only. This extension is intended for use only with MySQL versions older than 4.1.3. It can be used with newer versions of MySQL, but not all of the latest MySQL server features will be available.
PHP’s mysqli Extension
PHP also offers a more recent mysqli extension, sometimes known as the MySQL improved extension. The mysqli extension is included with PHP versions 5 and later and was developed to take advantage of new features found in MySQL versions 4.1.3 and newer. mysqli provides both an object-oriented and a procedural interface.
Why to Use the mysqli Extension
The PHP development team strongly recommends that if you are using MySQL versions 4.1.3 or later you should use the mysqli extension rather than the original mysql extension. The key reasons for using mysqli over mysql are:
- Object-oriented interface
- Support for Prepared Statements
- Support for Stored Procedures
- Support for Multiple Statements
- Support for Transactions
- Enhanced debugging capabilities
- Embedded server support
Additionally the mysql extension is no longer being actively developed by the MySQL development team, whilst the mysqli extension under active development. It also supports multiple statements and character sets, whereas the mysql extension does not.
But…
Although the PHP development team strongly recommend that you use the mysqli extension if you’re using MySQL versions 4.1.3 or later, there is a debate about speeds differences between the two extensions. There is evidence (example) to suggest that although under certain circumstances mysqli is faster, for everyday use mysql actually performs better in terms of speed than its more modern counterpart!
However, the tiny speed advantage to using mysql rather than mysqli is probably outweighed by the newer extension’s benefits.
Conclusion
Although it might be true that mysql is ever so slightly faster than mysqli, it’s still worth following the PHP development team’s advice and using the newer extension. mysqli gives you access to the features available in MySQL versions 4.1.3 or later and it’s more flexible than the mysql extension. In addition to that, as the mysql extension is no longer under active development by the MySQL team there is no guarantee that it will be included in future PHP releases.


