making bbPress (and WordPress) work better!

How to put MySQL functions back into PHP 7

PHP 7 will go “release candidate” on August 20th 2015 which is very exciting because it will instantly be twice as fast as PHP 5.6 (and all previous versions). PHP7 gives HHVM a run for the money and takes 5 minutes to compile instead of hours for HHVM.

But there is a catch – if you have any legacy code that uses the mysql_* functions, they will stop working entirely in PHP 7. Not just a warning, not just deprecated, but gone, fatal.

However, it is easy to get them back without using a wrapper or modifying your code…
I wouldn’t plan on using this solution far past 2016 but it will keep your legacy code running in the meanwhile.

Simply compile the mysql function as a php pecl extension and add one line to your php.ini

First get the mysql extension source which was removed in March:


cd ext/
git clone 	 mysql	 --recursive


cd ext/
svn co mysql

(same code, same timestamp, now static and likely will never be updated again)

Then compile the code on your server:

make install

The “make install” should announce where it is copying – copy that path. It will likely be /usr/local/lib/php/extensions/no-debug-non-zts-20151012/ for PHP 7 but it could vary depending on your server setup.

Then edit your php.ini
Somewhere either in the “Extensions” section or “MySQL” section, simply add this line:

extension = /usr/local/lib/php/extensions/no-debug-non-zts-20151012/

(change path if needed)

Restart PHP and mysql_* functions should now be working again. Check your php error log if you have any problems (should check it periodically anyway). You will also see the MySQL section back in your phpinfo page.

Alternately, compile the mysql support into the PHP binary

It is also possible to compile PHP with the mysql extension built right in, so you don’t need to change the php.ini file – follow these instructions:

Basically you put the mysql source under ext/mysql then clear out any existing configure in the php source root and force buildconf to make a new configure. Then compile php including –with-mysql on the ./configure command line.

Note that PHP 7 deprecates several other functions and is more strict about other things like classes, so you may need to tune your log to be less harsh about warnings.

error_reporting = E_ALL & ~E_STRICT & ~E_DEPRECATED & ~E_NOTICE

But don’t ignore the warnings entirely because “deprecated” will one day turn into completely missing and fatal like mysql functions.

45 responses

  1. Pingback: PHP 7.0 Final will ship November 12th 2015 | _ck_ says...

  2. Pingback: Der PHP 7 Migration Guide -

  3. Anonymous

    December 8, 2015 at 5:21 am

  4. Pingback: PHP7'ye güncelleme nasıl yapılır?

  5. Pingback: Wordpress使用PHP7.0更新紀錄 - 某數's blog

  6. thank you, it worked, i have mysql extension on php 7.0.3

    March 4, 2016 at 12:27 pm

  7. Gianluca

    In these wrappers all mysql and ereg functions are rewritten using mysqli and pcre

    March 15, 2016 at 1:50 pm

  8. Pingback: - » Verwendung von MySQL Funktion in PHP 7

  9. You’re my personal Jesus Christ.

    Thanks a lot!

    April 29, 2016 at 10:19 pm

  10. I haven’t understand from the line:

    Then compile the code on your server:

    May 16, 2016 at 7:40 am

  11. Anonymous

    How to put MySQL functions back into PHP 7 for WINDOWS

    May 31, 2016 at 7:15 am

    • These instructions are mostly meant for linux environments but if you knew how to compile on Windows, in theory it should also work. Alternately you could use the prebuilt php libraries for Windows from Jan-E on

      They also include the mysql functions in their builds.

      May 31, 2016 at 7:48 am

      • Anonymous

        Thanks a lot. It works!

        May 31, 2016 at 10:41 am

  12. Really? You want to use mysql_* functions? 😀

    June 10, 2016 at 12:36 am

  13. Pingback: Jak rozfungovat staré mysql_* funkce v PHP7 – Martin Janeček

  14. Pingback: Alte MySQL-Extension unter PHP 7 verwenden « Kreativrauschen

  15. Anonymous

    Tested, works. Thanks a lot!!

    July 29, 2016 at 3:46 am

  16. sandhya

    it isn’t working for me..
    make install perfectly worked. it gives output path like: /usr/lib/php/20151012. but i don’t know to what path i should change in extension path in php.ini file.
    the default was, extension=/path/to/extension/

    August 27, 2016 at 4:16 am

  17. sandhya

    can anyone please help?

    August 27, 2016 at 5:26 am

  18. Wow, it helped me a lot, thx for this article! 😀

    October 31, 2016 at 2:53 pm

  19. Thank you very much, 🙂

    November 4, 2016 at 3:48 am

  20. Hacking Tutorials

    Thanks a lot!

    November 26, 2016 at 1:54 pm

  21. antonio

    it’s work!!!
    thank you so much. i’ve tried to resolve this problem by a week, but i didn’t.

    i have changed from your tutorial the command ./configure in

    ./configure –with-php-config=/usr/bin/php-config


    February 1, 2017 at 12:08 pm

  22. Alinamib

    Куплю Квартиру Севастополь еще
    Севастополь Аренда Офисов

    March 1, 2017 at 5:19 pm

  23. how to convert mysql code into mysqli quickly ? i have a site based on mysql and not working now because i coded into mysql please urgent help required

    March 6, 2017 at 5:46 am

  24. Many thanks, this site is extremely practical.|

    March 14, 2017 at 9:17 am

  25. Ahmad

    That’s amazing 🙂
    I was thinking of creating a wrapper for it.

    March 28, 2017 at 12:05 pm

  26. Hey there, this worked for me and it totally saved my life so much!! !!!! But, strange thing when running PHP from the command line I still get the Call to undefined function mysql_connect()

    August 8, 2017 at 4:40 pm

  27. hey I figured it out!! 😀 there are 2 php.ini one under /etc/php/7.0/apache2 and there is another for cli under /etc/php/7.0/cli. I just needed to add the extension line in the cli php.ini as well and it made the extension work on php cli as well 😀

    August 8, 2017 at 5:01 pm

  28. The extension wasn’t working for me so

    I created a file
    nano /etc/php.d/50-mysql.ini

    with content

    ; Enable mysql extension module

    not it’s working THanks!

    September 18, 2017 at 3:22 am

  29. Anonymous

    thanks, working with installled fengoffice

    October 24, 2017 at 9:16 pm

  30. Andreas S.

    Hey there,

    I know it’s pretty sick to still use deprecated mysql_ext on PHP 7++ … but as i’m stuck with a mega-old-mega-big Monolith PHP App I’m forced to do so (at least at the moment until it get’s completely rewritten …)

    The App is working since years without Probs with PHP 7.0 – but now I try to upgrade it to PHP 7.1 (or even 7.2 later) … but I got Problems compiling the old mysql Ext with PHP 7.1

    /dl/mysql/php_mysql.c: In function ‘zif_mysql_list_fields’:
    /dl/mysql/php_mysql.c:1760:19: warning: assignment makes pointer from integer without a cast [enabled by default]
    if ((mysql_result=mysql_list_fields(mysql->conn, table, NULL))==NULL) {
    if ((mysql_result=mysql_list_fields(mysql->conn, table, NULL))==NULL) {
    php_error_docref(NULL, E_WARNING, “Unable to save MySQL query result”);

    /dl/mysql/php_mysql.c: In function ‘zif_mysql_fetch_lengths’:
    /dl/mysql/php_mysql.c:2372:14: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
    if ((lengths=mysql_fetch_lengths(mysql_result))==NULL) {
    if ((lengths=mysql_fetch_lengths(mysql_result))==NULL) {

    When I comment those Lines – it compiles fine and it works … but of course those 2 Functions making Problems …

    Anyone here who could give me a Hint on how to fix those 2 Code Fragments to get it to work with PHP 7.1?


    February 5, 2018 at 7:15 am

    • I’m not using 7.1 yet but those two “errors” appear to only be warnings from undeclared variable types now being caught by a better compiler – an oversight but not fatal – does it finish compiling and run/pass tests?

      February 7, 2018 at 3:34 pm

      • Andreas S.

        unfortunately not really … also found our what the Problem is … mysql_list_fields() was removed from mysqlnd … so no wonder it’s not working anymore … 😉

        As it’s getting harder to compile with every PHP Version I’ve gone another way and used a mysql –> mysqli wrapper … took me some Hours and everything was working fine with mysqli … even this is a Project with some Million LOCs … 😉

        February 7, 2018 at 3:45 pm

      • I will be sure to follow your other websites. This article is a bit too advanced for me so is there anything you’d recommend for those just starting out? Thank you so much for clearifing that third point, I’ve been really confused about that for a long time. I’m always browsingon the internet for insights that will help me.

        December 20, 2018 at 2:27 am

  31. itsystemid

    Reblogged this on carasimpeldotcom.

    April 25, 2018 at 5:57 am

  32. Pingback: How get all values in a column using PHP? - ExceptionsHub

  33. Jauh


    August 25, 2018 at 4:21 am

  34. Nice guide. I understand why they removed the mysql_query (etc) functions, but for some legacy sites it is useful to be able to continue to use them.

    September 28, 2018 at 1:03 pm

  35. I forgot to say – this worked today (for php7.2 on centos)

    September 28, 2018 at 1:04 pm

  36. Pierre-Verthume Larivière

    Thanks Antonio you are right, I had to add the with php config option because my Ubuntu came with php 7.2 but I switched to php 7.1 manually to run certain picky frameworks. didnt work until I add the sudo ./configure –with-php-config=/usr/bin/php-config
    note I think the forum transforms two dashes into 1 long weird dash so if you copy this and it does not work that might be why.

    Also in my case it was a little more complex. I had switched php version and /etc/alternatives my php pointed to 7.1 but both phpize and php-config still pointed to 7.2 that made it not work. Had to update those 2 as well with:
    sudo update-alternatives –set php-config /usr/bin/phpize7.1
    sudo update-alternatives –set php-config /usr/bin/php-config7.1

    Then was able to ./configure with the Antonio option and finally it worked for me!! Dont forget to add the extension both for apache and cli php.inis!! so it will work in php running from the command lines (maybe in cronjobs etc..)

    February 14, 2019 at 11:16 am

  37. Bart Coninckx

    This does not work on CentOS 7. First problem is dat you need to install php-devel. Secondly, the resulting so file will not load: cannot open shared object file: No such file or directory), /usr/lib64/php/modules/ (/usr/lib64/php/modules/ undefined symbol: mysqlnd_get_client_info)) in Unknown on line 0

    May 15, 2019 at 4:21 pm

    • Bart Coninckx

      The solution is to install php-mysqlnd

      May 15, 2019 at 5:05 pm

  38. Pingback: How get all values in a column using PHP? – Fix Code Error

  39. Pingback: How get all values in a column using PHP?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s