Convert mysql_query to mysqli_query, migrate, PHP?

Convert mysql_query to mysqli_query, migrate, PHP?

Here are some good examples of how you can adapt your code.

Old config, connecting to the database:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
 
$dbadress = "localhost";
$dbname = "db";
$dbusername = "root";
$dbpass ="";
 
$connection = mysql_connect($dbadress, $dbusername, $dbpass);
mysql_query($sql, $connection);
}
 
?>

becomes

2
3
4
5
6
7
8
9
10
11
<?php
 
$dbadress = "localhost";
$dbname = "db";
$dbusername = "root";
$dbpass ="";
 
$connection = mysqli_connect($dbaddress, $dbusername, $dbpass, $dbname) or die ('cannot connect to the SQL server');
 
?>

Just notice that the first variable from the parenthesis is the $dbaddress (localhost) and the last is the name of the database.

Another example with the old mysql_fetch_array.

Old version:

3
4
5
6
7
8
9
10
11
<?php
 
require ('config.php');
 
$requestSQL = mysql_query("SELECT * FROM table where id = 1 order by id ASC LIMIT 500", $connection);
while ($row = mysql_fetch_array($requestSQL))
	{
        }
?>

New version with mysqli:

4
5
6
7
8
9
10
11
12
<?php
 
require ('config.php');
 
$requestSQL = mysqli_query($connection, "SELECT * FROM table where id = 1 order by id ASC LIMIT 500");
while ($row = mysqli_fetch_array($requestSQL))
	{
        }
?>

So just change the location of the $connection variable and put mysqli instead of mysql.

Leave in the comments if you have questions or anything to add.

Add a comment: