PHP Classes

Or a Quick and Dirty solution

Recommend this page to a friend!

      PHP MySQL to MySQLi  >  PHP MySQL to MySQLi package blog  >  How to Convert MySQL ...  >  All threads  >  Or a Quick and Dirty solution  >  (Un) Subscribe thread alerts  
Subject:Or a Quick and Dirty solution
Summary:Recreate the mysql_ functions with an include file
Author:Colin McKinnon
Date:2015-11-23 22:33:10

  1. Or a Quick and Dirty solution   Reply   Report abuse  
Colin McKinnon - 2015-11-23 22:33:10
Given the near one-to-one mapping between mysqli_* and mysql_* functions, its possible to create a shim for backward compatibility. There is a good example at

Of course this mask out some of the extended functionality available with MySQLi (like asynchronous queries) but means that you can easily get the security and performance benefits of more recent PHP versions without having having to re-implement a large legacy codebase - just add the include directive as an auto-prepend. The only caveat (which Dave's excellent article omitted to mention) is that you can no longer vaildate a connection using is_resource() but the dotpointer documentation explains how to fix that quickly and painlessly.

  2. Re: Or a Quick and Dirty solution   Reply   Report abuse  
Dave Smith - 2015-11-24 00:28:54 - In reply to message 1 from Colin McKinnon
I just checked out the site you provided a link to and the 'shim' they provide is similar to my PHP MySQL to MySQLi package that the article mentions near the end.

I did not look at their code, so I can't comment on it specifically, however they did mention that only a few of the common function replacements had been tested. Whether you use their package, or mine, doesn't matter much to me, both are released into the public domain as a service to hopefully curtail some of the blow back that will occur when shared hosts upgrade to PHP 7 and legacy code stops working.

I have been quite vocal, and I will state it again here, that these plug and play solutions are only a stop gap and should not be considered the cure. It is important that legacy code be update to a supported mysql extension, mySQLi or PDO, as soon as possible.

You are correct, my article is missing the mention of is_resource, so I can cover that quickly right here. With some additional considerations that dotpointer did not touch upon.

Since MySQL is a resource and MySQLi is an object, legacy code using the function is_resource to validate the connection must be changed when you migrate to MySQLi. Here are some points to consider when making this change.

1) is_resource is used for resources other than MySQL, so do not do a search and replace. You must ensure that the resource being evaluated is a MySQL resource, and then you can make the change.

2) Replace is_resource with is_object to evaluate whether the argument is an object, however make sure you also implement the next consideration.

3) mysqli_connect will return an empty object if the connection fails, so is_object will evaluate to true even if the connection failed. Right after you use MySQLi to establish a connection you MUST use mysqli_connect_error or mysqli_connect_errno to validate the connection, then there will be no surprises with the subsequent is_object checks.

Thanks for taking the time to comment,