Emulate PHP’s original mysql extension with mysqli mapping functions


This is a mirror of

Purpose

Redefines deprecated or missing mysql_ functions and calls mysqli_ functions for PHP5.5+.

Problem

As of PHP5.5 the original mysql extension for PHP and its provided procedural functions for PHP is marked as deprecated and may be removed in future releases of PHP.

Websites and other PHP enabled projects that use these functions (like mysql_connect, mysql_query etc.) may not work properly after this change in PHP. This because the required functions now trigger an E_DEPRECATED error in PHP 5.5 and in future releases even cause errors as the functions do not exist anymore, breaking the bridge between your code and your database.

Solutions

Change ALL the mysql_ functions to their corresponding mysqli_ function. Not all mysql_ functions have mysqli_ versions and if they do, the syntax is often changed. Arguments are swapped, so mysql_example(arg1, arg2) may now look like mysqli_example(arg2, arg1) or even mysqli_example(arg2, arg1, arg3).
Run automatic conversion tool on the source that do the change. Have not tried this.
Disable the mysql extension, write a migration library that mimics the mysql_ functions and calls mysqli_ functions. This is what this library is about.
How the library works

If the mysql extension is missing but the mysqli extension is available, then it defines new mysql_ functions that executes the corresponding mysqli_ functions.
If the original mysql extension is there, then not much is done, as all is fine and dandy (except for E_DEPRECATED warnings, which must be neglected manually).
If none of the mysql or the mysqli extensions are loaded, then it dies, halting with an error.
The library defines many mysql_ functions that do not have any corresponding mysqli_ alternative, by combining mysqli_ functionality or by fiddling in other ways.

Usage

The library is a .php-file that you include into the top of the source:

<?php require_once("mysql.php"); ?>

Download

Click here to download the library
(unzip it)

Things to take into consideration

mysql passes resources but mysqli passes objects, so comparisons done in your source with is_resource() may no longer work. Example:

<?php
# mysql
if (is_resource($result)) {...}
# mysqli
if (is_object($result)) {...}
?>

This is an fairly easy search-and-replace operation if you compare to fix up all mysql_ functions to mysqli_ functions.

To ease this even more and make your code backward compatible I have bundled a is_mysql_resource() function that checks if mysql is loaded, then calls is_resource(), or if mysqli is loaded then it takes is_object() - or if none, then responses with false. You replace is_resource() with is_mysql_resource().

Resources and objects are not the same, so if you do exotic stuff with your resources, then you may have more to edit.

No extensive testing has yet been done (2013-09-26). Proved to be working functions are mysql_connect(), mysql_query(), mysql_close(), mysql_num_rows(). Some are not tested at all as I have not used them in the past, but they pass PHP lint check.

Addendum of mirror's author: Successfully tested (implemented in): and (Oct. 1, 2014)

Some functions may be missing, and some may (but should not) return data that is not equal to the original mysql_ versions.

If you want to get rid of the E_DEPRECATED, you have some options:


mysqli is faster than mysql, so your applications may be faster too.

The goal is to have a library that emulates ALL the functions and that returns the SAME data as the original mysql_ versions.

Please share and improve, mail me at dotpointer-at-gmail.com if you have improved it and like to share your work here.

Another compatibility issue: Fix htmlspecialchars() in PHP 5.4+ für Latin1 . . .

Download

Content Management System u5CMS

Service

English⇄German Translation Service

Blumen

blumen-bern-maarsen.ch