How to change mysql to mysqli?
The first thing to do would probably be to replace every mysql_*
function call with its equivalent mysqli_*
, at least if you are willing to use the procedural API -- which would be the easier way, considering you already have some code based on the MySQL API, which is a procedural one.
To help with that, the MySQLi Extension Function Summary is definitely something that will prove helpful.
For instance:
mysql_connect
will be replaced bymysqli_connect
mysql_error
will be replaced bymysqli_error
and/ormysqli_connect_error
, depending on the contextmysql_query
will be replaced bymysqli_query
- and so on
Note: For some functions, you may need to check the parameters carefully: Maybe there are some differences here and there -- but not that many, I'd say: both mysql and mysqli are based on the same library (libmysql ; at least for PHP <= 5.2)
For instance:
- with mysql, you have to use the
mysql_select_db
once connected, to indicate on which database you want to do your queries - mysqli, on the other side, allows you to specify that database name as the fourth parameter to
mysqli_connect
. - Still, there is also a
mysqli_select_db
function that you can use, if you prefer.
Convert MySQL to MySQLi in PHP
Use mysqli_query
- syntax almost the same, but if your use procedural style, first parameter - database link:
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
$query = mysqli_query($link, "INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
Or object style:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = $mysqli->query("INSERT INTO users VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
Also you should not use variables direct in the sql query, use parameters binding:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$stmt = $mysqli->prepare("INSERT INTO users VALUES ('',?,?,?,?,?,?,'0')")
$stmt->bind_param('ssssss', $un, $fn, $ln, $em, $pswd, $d);
$stmt->execute();
$stmt->close();
How do I migrate my site from mysql to mysqli?
I advise you to read through this. A lot of helpful information for what you want to do, plus a tool to help. See: Converting to MySQLi.
It's just the PHP that changes.
Updating from MYSQL to MYSQLI
You can download a converter tool from here:
https://github.com/philip/MySQLConverterTool
The code it generates is pretty gross, mainly because of the way it implements the default database link argument with a $GLOBAL
variable. (This also makes it easy to recognize when someone is using code that's gone through the converter.)
There's also a MySQL Shim Library located here:
https://github.com/dshafik/php7-mysql-shim
how to do mysql to mysqli
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.
Related Topics
Convert Svg Image to Png With PHP
Php Json String, Escape Double Quotes for Js Output
How to Get Multiple Data from Two Different Table from One Database in PHP
How to Convert Text to Svg Paths
How to Fix Undefined Offset: 0 in Laravel Blade
Remove Country Code from Phone Number
How to Convert a 13 Digit Unix Timestamp to Date and Time
Laravel How to Return Single Column Value Using Query Builder
How to Delete Multiple Redis Keys With the Same Pattern in PHP Using Phpredis
Codeigniter - Show Information in Modal Window
How to Add Whatsapp Share Button to PHP Website
How to Use Bcrypt For Hashing Passwords in PHP
Laravel Eloquent How to Get the Minimum Value That Is Not Null
Codeigniter - Call to a Member Function Result() on Boolean In
Invalid Argument Supplied For Foreach()
Reference - How to Handle Namespaces (Tags and Attributes With a Colon in Their Name) in Simplexml