1

I am upgrading my PHP scripts to use mysqli and it necessitates changing thousands of lines where the variables have exchanged position. For example:

mysql_query($query, $db)

now must become

mysqli_query($db, $query)

Is a regular expression the best way to quickly changes thousands of lines? If so, could someone help me create the regex in order to swap these? I've already changed the function calls for mysqli, but the function parameter swapping is beyond my find/replace skill.

I'm using Visual Studio 2015 find/replace.

Dark Step
  • 13
  • 3
  • 1
    you have thousands of query executions in your project??? – BeetleJuice Aug 09 '16 at 17:03
  • See this [question](http://stackoverflow.com/questions/26476162/can-i-blindly-replace-all-mysql-functions-with-mysqli). – brevis Aug 09 '16 at 17:04
  • something like this should work in regexp find the following 'mysql_query\(\$(.+)\,\$(.+)\)' replace with 'mysqli_query\(\$$2\,\$\1\)'. Sorry this is just a pointer not an answer. Especially the fact that I don't know what you are using for find and replace makes it hard. E. g. sometimes to replace you use \1 not $1. It's also easier if you do it before changing the function name. – MotKohn Aug 09 '16 at 17:16
  • I didn't create the project, but I'm responsible to maintain it. It's a sports web site and the queries mostly follow this format: `$GetStats = mysql_query($query_GetStats, $db) or die(mysqli_error());` I'm using Visual Studio 2015 to find/replace. – Dark Step Aug 09 '16 at 17:31

2 Answers2

0

I dont think it need regex it worked in sandbox

open each file and search for mysql_query($query, $db) every time you encounter string run str_replace('mysql_query($query, $db)','mysqli_query($db, $query)', 'mysql_query($query, $db)' );

$test = 'mysql_query($query, $db)';
       echo str_replace($test,'mysqli_query($db, $query)', 'mysql_query($query, $db)' );
echo $test;
brad
  • 993
  • 7
  • 10
0

Replace

mysql_query\s*\((\$\w+),\s*(\$\w+)\);

with:

mysqli_query($2, $1);

The non-escaped parentheses in the regular expression create capture groups, and $1 and $2 in the replacement pick up what was matched by them.

This will only work for statements like you've posted, where the query is a variable, since \$\w+ just matches a variable name. It won't work if you have literal strings like

mysql_query("SELECT ...", $db);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • It doesn't seem to find any. The full statement usually looks like this, and there are not many instances that use literal strings: `$GetStats = mysqli_query($query_GetStats, $simhl) or die(mysqli_error());` Does your regex need a modification to find/replace for this case? – Dark Step Aug 09 '16 at 17:33
  • Thanks Barmar! This did work with some minor changes to suit my situation. As you said, it does not find the "..." or $blah_blah, but it helped immensely. – Dark Step Aug 09 '16 at 18:05
  • I'm not sure why it doesn't find `$blah_blah`, since `\w` matches letters, numbers, and underscore. – Barmar Aug 09 '16 at 19:24