Archive for PHP

Upgrading PHP Code from MYSQL to MYSQLi

I have been coding in PHP for many years. So many I’ve lost count. So I have written more than a few mysql_connect and mysql_query commands in more web pages than I can shake a stick at. I never spent much time thinking about it till I heard that the MYSQL was being deprecated in PHP 5 in favour of the MYSQLi extension that was to take its place.

Yesterday I took some time to build a new feature on an old website, and used MYSQLi in earnest for the first time. It was a pretty good experience over all. MYSQLi feels familiar and friendly, even if it changes the order of some arguments, and it is far more OOP-friendly than the old extension.

MYSQLi stands for MYSQL improved, and in many ways it is improved. I haven’t had the need for transactional statements, but many have, and now the functionality is there. There are other things I can easily do with MYSQLi that took handstands and backflips to do with the old MYSQL extension. I’m particularly happy with the ability to have multiple statements via mysqli_multi_query().

This weekend, I plan on finishing my new features, then travelling back in time to convert my old publishing site code to be compatible with MYSQLi. It probably won’t be fun. The site was built in chunks, on the fly, and usually without much of a plan, over the course of thirteen years. It’s a bit of a logistical mess. I’m starting with the newest sections, and a fresh database, and will work it a bit at a time. It’s probably a good time to upgrade all of the code, not just the databases, and make the site a little more cohesive and the code much more object oriented.