Skip to content

Instantly share code, notes, and snippets.

@rubo77
Last active December 13, 2023 23:58
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rubo77/1db052edd8d723b59c79790b42635f1e to your computer and use it in GitHub Desktop.
Save rubo77/1db052edd8d723b59c79790b42635f1e to your computer and use it in GitHub Desktop.
A php include that replaces all mysql functions with the corresponding mysqli functions

If you have any questions open an issue there or enhancements as Pull Request

replacement for all mysql functions

Be aware, that this is just a workaround to fix-up some old code and the resulting project will be more vulnerable than if you use the recommended newer mysqli-functions instead. So only If you are sure that this is not setting your server at risk, you can fix your old code by adding this line at the beginning of your old code:

see: https://stackoverflow.com/a/37877644/1069083

@mythusiva
Copy link

Thanks! Very helpful.

Added a few more methods here:
https://gist.github.com/mythusiva/0523cf1fcdd169fe9bce5242ca2e78ef

@ianmin2
Copy link

ianmin2 commented Jan 10, 2017

Brilliant!

Thank you very much.

I am using a framework with php7.0 for the first time and was wondering why it kept on telling me that the mysql_pconnect function is unavailable.

@jjjjcccjjf
Copy link

Thanks. Really helpful

@goryny4
Copy link

goryny4 commented Apr 17, 2017

@mythusiva, thanks!!!

@igbominadeveloper
Copy link

Please how do i use this file?

@atefBB
Copy link

atefBB commented Apr 27, 2017

thx, you save my day !

@mattzamec
Copy link

Wanted to leave another big thanks ...

@ashok133
Copy link

Life saver. Thank you!

@mattdconnell
Copy link

mattdconnell commented Feb 19, 2018

Greatly appreciate the effort here.

In the process of porting a legacy application to modern PHP, I found a few more functions missing that you may wish to add. Here is how I've implemented them.

function mysql_escape_string($s) {
    global $dbconnect;
    return mysqli_real_escape_string($dbconnect,$s);
}

function mysql_fetch_row($e) {
    return mysqli_fetch_row ($e);
}

function mysql_errno($db) {
    return mysqli_errno ($db);
}

function mysql_fetch_object($erg) {
    return mysqli_fetch_object ($erg);
}

function mysql_field_name($result,$i) {
    return mysqli_fetch_field_direct($result,$i);
}

function mysql_free_result($result) {
    return mysqli_free_result($result);
}

@rubo77
Copy link
Author

rubo77 commented Oct 24, 2018

@mattdconnell: thanks, I added it.


Note:
mysql_affected_rows() does still not work like this if you call mysql_affected_rows() without argument!

@rubo77
Copy link
Author

rubo77 commented Oct 24, 2018

@mythusiva: I added your improvements too ;)

@skierdy
Copy link

skierdy commented Jan 23, 2019

Thanks a lot!

@nemodibia
Copy link

Maaaaann.... you saved a life here. You saved me lots of time. Bought you fruits anytime you pass by Kampala 👍 :-)

@bome
Copy link

bome commented Feb 26, 2019

brilliant, thanks for this. Two improvements:

1) mysql_select_db() can be called without 2nd parameter:

  function mysql_select_db($db, $dbconnect_link = null){
    global $dbconnect;
    if ($dbconnect_link == null) {
      $dbconnect_link = $dbconnect;
    }
    return mysqli_select_db ( $dbconnect_link, $db );
  }

2) mysql_fetch_array() can be called with a second argument:

  define("MYSQL_ASSOC", MYSQLI_ASSOC);
  define("MYSQL_NUM", MYSQLI_NUM);
  define("MYSQL_BOTH", MYSQLI_BOTH);

  function mysql_fetch_array($e, $result_type = MYSQL_BOTH){
    return mysqli_fetch_array ($e, $result_type );
  }

@rubo77
Copy link
Author

rubo77 commented Mar 2, 2019

moved to GitHub: https://github.com/rubo77/php-mysql-fix

I added your enhancements there.

please open issues an PRs there if you have any more solutions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment