0

We know that the other drivers are depreciated and from php7 its recommended to use mysqli methods. The problem is i came across codebase from a long time ago from one of my employer and he wants that updated.

Now there are around 3K files and the previous developer did a great job at putting in 3-5 mysql_query calls in almost every file. How would you recommend updating all of them at once, maybe some kind of method overide trick if someone has had the same issue in future.

Currently i am working with 2 vim macros that find mysql_ keyword and replace with mysqli_ and the other one which adds $con as first parameter to the method mysqli_connect.

Even with this automation, i still need to open all files individually. Any better hack is appreciated that would prevent me from opening all the files manually and making changes.

georoot
  • 3,557
  • 1
  • 30
  • 59
  • The title reads *"update php mysql to mysqli?"* and you state *"Let me explain, this has nothing to do with working of mysqli"* - I think you need to remove that part in your question. – Funk Forty Niner Mar 23 '17 at 00:20
  • Now i think it should be better – georoot Mar 23 '17 at 00:21
  • You could give the shim for ext/mysql in PHP 7+ a try https://github.com/dshafik/php7-mysql-shim - that may help you for a while till you convert everything over to mysqli or pdo. – Funk Forty Niner Mar 23 '17 at 00:22
  • I'd suggest making a class that handles the queries, and slowly re-write the files with queries in to call the class and use it's methods (and probs some base class too etc) Instead of just changing from one bad code design to another :(. Otherwise in the future someone else will be saying "this codebase is littered with mysqli calls, it's a nightmare to maintain and change, how do I..." – James Mar 23 '17 at 00:24
  • then there's https://github.com/philip/MySQLConverterTool which by chance, also mentions about the shim in there. – Funk Forty Niner Mar 23 '17 at 00:24
  • You could open the folder in visual studio and run your replacement against all of them at once. – Brett Harris Mar 23 '17 at 00:26
  • @Fred-ii- the library got warning as a stop, and either way would have to replace the entire content. twice the work... i was thinking something along the lines of piping the entire file directory using unix `tree` and the going somewhere with that – georoot Mar 23 '17 at 00:26
  • @BrettHarris linux user here pretty sure VS not an option. Is the same thing possible in sublime, if you know by any chance ? – georoot Mar 23 '17 at 00:27
  • No it's not, I tried it in there before suggesting VS, I wish it did too lol. – Brett Harris Mar 23 '17 at 00:29
  • This looks like what you want http://stackoverflow.com/questions/11392478/how-to-replace-a-string-in-multiple-files-in-linux-command-line – Brett Harris Mar 23 '17 at 00:30
  • Using sed or some other search and replace to replace mysql to mysqli? Entirely depends on what queries you have. It's likely not that simple even for the most basic queries. The two functions are not the same, there are different functions with different requirements. Also, without going through your code carefully, you are open to errors, but mainly not changing the queries you miss out on the benefit of parametrised queries. – James Mar 23 '17 at 00:44
  • Btw; if any of the files contain `mysql_real_escape_string()` or any other function in mysqli_'s equivalent that requires a db connection such as `mysqli_real_escape_string()` or `mysqli_affected_rows()`, or `mysqli_insert_id()` for instance, you may have problems. Just saying and hoping you took that into consideration. Also. If this involves any type of login or user input, I'd highly suggest you use a prepared statement. It may take some time but your client will appreciate the time and effort put into it. @georoot – Funk Forty Niner Mar 23 '17 at 00:52
  • This just keeps getting more complicated.. I was really hoping on not rewriting the entire codebase :( – georoot Mar 23 '17 at 01:07

2 Answers2

1

Here is a short way to update all the functions in codebase.

Replacing mysql_ with mysqli_

So the macro vim macro i used for this is

:%s/mysql_/mysqli_/g

This seems to be working fine with almost all functions

Changing methods that need additional params

mysqli_query needed another parameter $con so i created a macro like

:%s/mysqli_query(/mysqli_query(\$con/g

This adds the $con variable.

Save macros to file

Now the above macros can be stored in a file using

"ap

where a is the macro name, similary repeat for b and save them as a.vim and b.vim

Run on all files

Now the simple trick is running macro on all files using bash script. I simply created a loop for that

for i in *.php;
  do
    vim -s ./macro/a.vim $i;
    echo $i;
  done

This would run a.vim macro on all files. Similarly repeat the step for b.vim.

Store and close the files

By default the vim will open the file after running macro, we need to save that file by default. Add the following at the end of macro script

:wq!

This will save and exit the php file after making changes making the entire process faster and autonomous

Note the above method works but you might need some changes depending on your codebase and how to methods are called.

georoot
  • 3,557
  • 1
  • 30
  • 59
-1

As a workaround, I created a small PHP include file, that recreates the old mysql_* functions with mysqli_*()-functions: fix_mysql.inc.php

rubo77
  • 19,527
  • 31
  • 134
  • 226