Fix error: mysqli_select_db / query expects 2 arguments, 1 given

I have upgraded MySQL to a higher version, but I am getting an error:
Fatal error: Uncaught ArgumentCountError: mysqli_select_db() expects exactly 2 arguments, 1 given

How can I fix it to make SQL queries work like in previous MySQL versions ?
0
give a positive ratinggive a negative rating
19 Sep 2022 at 10:12 AM
Hi,

The error is related to using of mysqli, which is different from mysql. Mysqli is newer and works in a bit different way. When using mysqli_select_db, mysqli_query or other mysqli functions, you have to add connection there, to use 2 parameters instead of 1.

Example of using mysql:

$database = "database_name";
$connection = mysql_connect("server_name", "user_name", "password");

mysql_select_db($database);

$sql = "SELECT product_name FROM products WHERE id_brand = 15 ORDER BY added DESC;";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);

Example of using mysqli:

$database = "database_name";
$connection = mysqli_connect("server_name", "user_name", "password", "database_name");

mysqli_select_db($connection, $database);

$sql = "SELECT product_name FROM products WHERE id_brand = 15 ORDER BY added DESC;";
$query = mysqli_query($connection, $sql);
$result = mysqli_fetch_array($query);

0
give a positive ratinggive a negative rating
25 Sep 2022 at 10:07 AM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us