1

php is about to stop support for mysql functions, and there are lots of tools/recommendations for how to upgrade to upgrade to mysqli from mysql. I came up with this design, and I wonder if there are any issues that my solutions can have:

  1. Create a file called translate.php and require_once it in all php files that use any mysql functions.
  2. Create global variable in that file and call it $_CONN
  3. overwrite default mysql function with the following (using overwrite_function which is not shown here)

$_CONN;        
        mysql_query($q)
        {mysqli_query($_CONN,$q);}

        mysql_error()
        {mysqli_error($_CONN);}

        mysql_connect($h,$u,$p)
        {$_CONN=mysqli_connect($h,$u,$p);}

        mysql_fetch_assoc($r)
        {mysqli_fetch_assoc($r);}

        mysql_fetch_array($r)
        {mysqli_fetch_array($r);}

Are there any issues with my solution?

apomene
  • 14,282
  • 9
  • 46
  • 72
Dimitri
  • 453
  • 3
  • 11
  • 3
    Problem with your solution is trying to "intelligently" solve your problem by using globals and renaming a few functions. Sadly, that's a hack at best and not a solid solution. The real answer to your question would be what Your Common Sense suggested - using PDO. However, that will probably open up new problems for you seeing that you most likely still use procedural code (which is ok unless it's spaghetti code, and procedural code with global variables is nearly always spaghetti code). If you are ok with having hacked spaghetti code running your app - then I guess you're fine. – N.B. Apr 10 '14 at 13:09
  • 1
    The main reason why I am considering my solution is because of the way my current environment is designed. there is one file, which has username,password, and mysql_connect function and thousands of php files that call queries(which include that one file). Unfortunately I do not have neither access, nor desire to mess with more than 2 files, which makes me seek a simple solution like this hack. – Dimitri Apr 10 '14 at 13:22
  • just keep with old mysql then – Your Common Sense Apr 10 '14 at 13:26
  • 2
    Also, in case you were wondering, this is called code rot. – Tom Macdonald Apr 10 '14 at 13:35

1 Answers1

4

To move from mysql to mysqli in a painless way you have to move towards PDO

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345