PHP 7 backwards compatibility library for mysql and ereg functions

The very commonly used mysql_* and ereg* PHP functions were removed in PHP 7. For very large codebases, refactoring the code to remove them just isn't practical. To that end, we have developed a compatibility library to enable existing websites and PHP scripts to keep running under newer PHP versions.

The code is available on GitHub under the Apache 2.0 Licence. We have included compatibility wrappers for all of the ereg* functions, the most commonly used mysql_* functions, and a couple of other useful functions as well. Using the code is as simple as cloning the repository, and either loading the wrapper using auto_prepend_file in php.ini, or using include()/include_once()/require()/require_once(). The library can safely be loaded multiple times. It can also be safely loaded into PHP 5 setups (e.g. in preparation for the switch to PHP 7). Make sure to read the README notes for instructions and limitations.

We welcome feedback and contributions to make the library more useful for everyone.


Today I found out that version 2.x of the CodeIgniter framework uses the old deprecated mysql_* functions. If you're stuck supporting a site based CodeIgniter 2.x, this backwards compatability library will allow you to continue hosting the site on modern versions of PHP.

John Greenbury - Sun, 25/04/2021 - 00:59


Hi Pete, thanks for this code. My very large web application works well in Php 5.3 but generates errors from 5.6 up. Does your library deal with most issues? Also, can other overrides be added if needed?

Hi John, without seeing the specific errors, I can't really answer your first question.

This compatibility library covers most of the commonly used deprecated mysql_* functions. It doesn't yet have complete coverage of all mysql_* functions. There is also some subtlety about the implementation of mysql_connect, which isn't totally backwards compatible in the case where you have multiple database connections open at the same time. We intend to fix this but it's quite a complex issue and we just haven't got the time to tackle it right now - this is a free library and as you can imagine, paid projects have to take precedence. The library also covers ereg* and split functions. There may well be other areas of incompatibility between PHP 5.3 and 5.6, and no, this library doesn't address them at all. It's not intended as a blanket backwards-compatibility fix. It's primarily intended to ease the transition away from deprecated mysql_* functions.

You can see a list of the mysql_* functions that haven't yet been implemented in the README at the bottom of If you search your codebase for these functions, you will know if there are any that need to be implemented. You will need to use a PHP backwards compatibility checker such as to pick up other potential issues. Since version 7.0, PHP publishes a full list of backwards incompatible changes for each release, but unfortunately these weren't published for the versions you're looking at.

As to the second question, yes, you could fairly easily add in other overrides. Either create a separate library that follows the same principles, or fork the library and add in the extra features. We would of course appreciate if you contributed any changes back so that everyone can benefit from them.

I hope that this is helpful to you. If you find that you need more backwards compatibility fixes and wish sponsor us to develop them as part of this library, please feel free to contact us using our contact form (link at the top right of the page). Thanks!

Add new comment