Iain's IT Solutions Ltd.

Iain's IT Solutions Ltd Mobile Nav Menu

Changing PHP extensions from MySQL to MySQLi


So I'm having to change all my procedural PHP code - upgrading the soon to be deprecated MySQL functions with the replacement MySQLi functions.

Unfortunately it's not just a case of adding an "i" as some of the parameters have changed.

MySQL_query($sql) to MySQLi_query($con, $sql)
This is annoying as it's the most common and it needs a new connection parameter adding.

MySQL_connect to MySQLi_connect
Replacing the 4th parameter of "true" with the database name.

MySQL_select_db($db, $con) to MySQLi_select_db($con, $db)
Swapping the parameters - so another annoying one. Although if I add the database name to the MySQLi_connect fourth parameter, I don't need this select the database line.

MySQL_insert_id to MySQLi_insert_id
Adding the connection parameter

MySQL_real_escape_string($str) to MySQLi_real_escape_string($con, $str)
Adding the connection parameter

I've done one site so far. One issue is that I have to update the entire site in one go - I can't do it in stages. All connections go through a single function - so as soon as that's changed - every page which calls it stops working. If I update all the other bits first, they won't work until the connection has been updated…so I'll need to think of some way to deal with this…

Update: 02Jan13
If a connection is created, then used in a function, its scope used to extend down into the function. This no longer happens. I've now to either pass the connection as a parameter into the function or add global $con; to the function - explicitly making it global.

No links allowed (to stop persistent spamming...)

Optional - enter if you want to be alerted when new comments are added