Automatically refactor legacy mysql functions to mysqli (PHP5 to PHP7)


I have to migrate a old PHP5 Project to a new Server running PHP7.
All mysql_* functions has been marked deprecated in PHP 5.5 and been finally removed in PHP 7.

So I have to refactor a large legacy code base.

Fortunately there is already a litte tool, which is unbelievably helpful for this task.
I just have to run these three lines.
git clone https://github.com/philip/MySQLConverterTool.git
cd MySQLConverterTool/
php cli.php -u -d "<pathToYourFiles>" -p "*.php"

Before:
<?php
$connect = @mysql_connect($dbhost,$dbuser,$dbpass);
if (!$connect) {
die('Page not available');
}
mysql_select_db($dbname) or die(mysql_error());
mysql_set_charset("utf8");
?>

After:
<?php
$connect = @($GLOBALS["___mysqli_ston"] = mysqli_connect($dbhost, $dbuser, $dbpass));
if (!$connect) {
die('Page not available');
}
mysqli_select_db($GLOBALS["___mysqli_ston"], $dbname) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
((bool)mysqli_set_charset($GLOBALS["___mysqli_ston"], "utf8"));
?>

As you can see there are a problem with the @ before the mysql_connect so you have to check these code locations manually.

All other files in this project where converted without any problems, I was surprised by myself 🙂


Archive

No Responses

Leave a reply

*

vier × 1 =