We have an app here that has been in development (and now in production) for more than a year. Which has in total over 500 mysql_*
calls.
Is it worth it to switch all of the mysql_*
in the code to mysqli_*
Is it worth chasing all the bugs that might (and most probably will) come about?
I see from questions like this: Changing this from MySQL to MySQLi? that just adding i
after every mysql*
call can lead me to alot of errors. Is it worth my time?
mysql_*
will probably around for the long haul (even amongst rumors of deprecation), so it it really worth any programmers time to methodically switch over?
See also this discussion
MySQLi supports both procedural interfaces and object oriented interfaces while MySQL supports only procedural interfaces. MySQLi supports stored procedure but MySQL does not. There is enhanced security and improved debugging features in MySQLi where this is comparatively lagging in MySQL.
The installation process with MySQLi not only easy, but is automatic when the PHP5 MySQL package is installed in Windows or Linux. MySQLi performs (slightly) faster than its competition. With non-prepared statements, the system performs ~2.5% faster, and ~6.5% faster with prepared ones.
The main advantage of PDO over MySQLi is in the database support. PDO supports 12 different database types, in opposition to MySQLi, which supports MySQL only. When you have to switch your project to use another database, PDO makes the process simpler.
It is possible to include both MySQL and MySQLi when connecting to a single database, but it is incredibly delicate and with large amounts of data being passed through it can get very messy and hard to control. it is best to use MySQLi in general in my opinion because it is much more secure and up to date.
Quoting the manual for ext/mysqli
:
The mysqli extension has a number of benefits, the key enhancements over the mysql extension being:
- Object-oriented interface
- Support for Prepared Statements
- Support for Multiple Statements
- Support for Transactions
- Enhanced debugging capabilities
- Embedded server support
Note: If you are using MySQL versions 4.1.3 or later it is strongly recommended that you use this extension.
If you need just one of those features and can afford the refactoring, then yes, go for it. If you dont need any of those features then dont do it. There is no reason to refactor if there is no benefits.
On a sidenote, the rumors are true. ext/mysql will likely be deprecated (although no one can say when at the time of this writing. It certainly wont be deprecated with 5.4. and it will likely be available as a pecl extension forever) In any case, you shouldnt be starting any new projects with ext/mysql anymore when you have a superior extension to start with.
Also see http://blog.ulf-wendel.de/2012/php-mysql-why-to-upgrade-extmysql/
In my opinion, the benefit of MySQLi is when it is used in an object-oriented fashion, and with prepared statements. You get some additional versatility from it using the procedural style too, such as nice wrapper functions around transaction handling, but I think not enough to justify unless you rewrite lots of your code to make use of them.
And if you were to undertake the effort to convert to OO code or prepared statements, you might as well convert to the more flexible PDO instead of to MySQLi.
Update Jan 2013
Just found this old answer, and in the Aug 2011 comment thread below I said it wasn't worth it to convert mysql_query()
calls to mysqli_query()
absent an accompanying move to prepared statements. It now IS necessary to start moving in that direction, as the mysql_*()
extension is deprecated as of PHP 5.5 and will eventually be removed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With