Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A php include that replaces all mysql functions with the corresponding mysqli functions

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

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

This comment has been minimized.

Copy link

commented Oct 2, 2016

Thanks! Very helpful.

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

@ianmin2

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Mar 20, 2017

Thanks. Really helpful

@goryny4

This comment has been minimized.

Copy link

commented Apr 17, 2017

@mythusiva, thanks!!!

@igbominadeveloper

This comment has been minimized.

Copy link

commented Apr 25, 2017

Please how do i use this file?

@atefBB

This comment has been minimized.

Copy link

commented Apr 27, 2017

thx, you save my day !

@mattzamec

This comment has been minimized.

Copy link

commented Apr 28, 2017

Wanted to leave another big thanks ...

@ashok133

This comment has been minimized.

Copy link

commented Jul 27, 2017

Life saver. Thank you!

@mattdconnell

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner Author

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

This comment has been minimized.

Copy link
Owner Author

commented Oct 24, 2018

@mythusiva: I added your improvements too ;)

@skierdy

This comment has been minimized.

Copy link

commented Jan 23, 2019

Thanks a lot!

@nemodibia

This comment has been minimized.

Copy link

commented Feb 19, 2019

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

@bome

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.