journal features
movie reviews
photo of the day

switching database extensions in PHP

the journal of Michael Werneburg

twenty-seven years and one million words

Toronto, 2019.04.07

I must be the very last person on Earth to migrate a substantial PHP project from the antique mysql_ functions (which went out with MySQL 5.something) to mysqli_. But in case there's anyone left on the old system, here's how I converted the many, many scripts that make up my CMS and delivery system. The following commands on the Linux command line were all I needed.

tar -cf before_attempted_migration.tar *php

for b in *php

do

perl -p -i -e "s/mysql_/mysqli_/g;" $b

done

for b in *php

do

perl -p -i -e "s/mysqli_query((.*), (.*))/mysqli_query(2, 1)/g;" $b

done

The second command is required because in the transition, the bright minds who create the PHP language seem to have fixed a needle/haystack ordering issue. Or perhaps they made one.

Once your code checks out (the functionality is the same as before your migration) you can delete the .tar file.

rand()m quote

In an unequal society, those who land on top want to believe their success is morally justified.

—Michael Sandel