Skip to content

Instantly share code, notes, and snippets.

@rubo77
Last active December 13, 2023 23:58
Show Gist options
  • 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

@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