Not to a specific query but all of software queries to be converted from mysql to mysqli due to the advent of PHP 7. It is a very old coding and so there are many occurrences as such. Piece of string or syntax is mysql_query($query, $link);to be converted to mysqli_query($link, $query); $link1, $link2 can be for different database connections.
-
Where have you declared `$link1` and `$link2`? It's the best if you specify `$link` in some config file and include it on every page – Indrasis Datta Oct 01 '16 at 10:16
3 Answers
Use regular expressions. I have not checked that my expression works as advertised, and regular expression syntax varies depending on the tool you use. But in general it should work this way:
- Search for
mysql_query\(("[^"]*"), *(\$[a-zA-Z0-9_]+)\)
- Replace with
mysqli_query($2, $1)
As an example, in Eclipse IDE, use "Search/File...", tick the checkbox "Regular expression", specify "*.php" as file name pattern and enter this as under "Containing text":
mysql_query\(("[^"]*"), *(\$[a-zA-Z0-9_]+)\)
Then hit the "Replace..." button and enter this under "With":
mysqli_query($1, $2)
Click Preview to check the results. If done correctly, this can update hundreds of files within seconds. Remember to test your code afterwards.

- 13,939
- 5
- 50
- 79
-
I have tested it and it is working. This is going to save my precious time, Axel! Thank you for your solution. – Indication Oct 01 '16 at 11:14
-
Axel, what if the query is like `mysqli_query("select field1, field2 from table where row1 = '".$var1."' and row2 = '".$var2."' limit 1", $linkx)`. How to pick and interchange them? – Indication Oct 02 '16 at 07:47
A dirty solution:
Firstly, remove your mysql
extension so the functions like mysql_query
will not be defined.
Secondly, write functions like this:
function mysql_query($q, $l){
mysqli_query($l, $q);
}
I mean, rewrite all mysql functions to call mysqli.
Then it may work well.
Of course, if your code have used so many mysql
features, then this may not work well. Good luck!

- 15,854
- 5
- 53
- 88
-
-
@EdvinTenovimas Thank you. If you like it, then you can give me a upvote. Thanks :) – ch271828n Oct 01 '16 at 10:54
-
1
-
It is good, but I need to replace code to save time. In your solution I would have to write the function and after that will have to write a code for each query to call that function individually. – Indication Oct 01 '16 at 11:17
For some reasons, solution from @Axel is not working on my version of the Eclipse.
To replace the code:
mysql_query($1, $2)
I have used this regex search and it worked:
mysql_query\((\$.*), (\$.*)\)
To replace it with:
mysqli_query($2, $1)

- 11
- 2