mysql - PHP function override?

135

I have the following situation of being between two versions of PHP during development. I've temporarily done the following below:

$oldphp = getdbVersion();
if($oldphp)
{
   $result = mysql_query($sql) or die(mysql_error());
}
else
{
   $result = mysqli_query($conn, $sql) or die(mysql_error());
}

Is there anyway to override these functions so you can call the new name but the old name is called automatically so the code would only have:

 mysqli_query ($sql);

but would see the correct function based on the server version?

I ended up using a wrapper so code checking doesn't need to be all over the place to check if the function exists:

function _mysql_query($connection, $findrecords)
{
   if(check_sql_version())
   {
      $result = mysql_query($findrecords);
   }
   else
   {
      $result = mysqli_query($connection, $findrecords);
   }
   if( ! $result)
   {
      die('Invalid query: ' . (check_sql_version())? mysql_error():mysqli_error());
   }
   return $result;
 }
305

Answer

Solution:

Appaprently you can override PHP built-in functions (using APD extension), but you should really have a good use case when that's something that you would want to do. The one in the question is not one such case.

If you need to support both mysql and mysqli extensions, than you should abstract database interaction using a set of classes and adapters, where each adapter implements a common interface used to interact with the database. There are many database abstraction libraries available, and you will probably find a one that meets your system requirements, while still providing access methods via both mysql and mysqli extensions.

ADODB is one of the classics. Being a legacy library itself, it will most probably work with the old versions of PHP that you have to support, while still being able to use mysqli extension as well. Docs are here:

http://phplens.com/lens/adodb/docs-adodb.htm

867

Answer

Solution:

You need to define wrappers for all the functions, there's a good example of how to do that here. In that case it provides backward compatibility, this is very useful if you have lots of legacy code you want to run in a newer version of PHP. However your situation is the opposite. You should be developing using mysqli provisioning a forward comparability shim.

To invoke the (forward) shim, simply...

if(!function_exists('mysqli_connect')) {
    require_once 'mysqli.inc,.php';
}

However I'm not aware of an existing forward shim for mysqli.

People are also looking for solutions to the problem: Amazon SNS using PHP SDK - createPlatformEndpoint "InvalidArgumentException" exception

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.