When attempting to execute a query with mysqli_query() in PHP, you may encounter an error stating that parameter 1 expects a mysqli object, yet a resource is provided. This discrepancy arises when you mix the mysqli and mysql extensions in your code.
To resolve the issue, ensure that you use the mysqli extension throughout your code. Specifically, replace the following lines in your provided code:
$myConnection= mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); mysql_select_db("mrmagicadam") or die ("no database");
with:
$myConnection= mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql"); mysqli_select_db($myConnection, "mrmagicadam") or die ("no database");
mysqli offers several advantages over the old mysql extension, including improved performance and security. It is highly recommended to switch to mysqli for your PHP database interactions.
The above is the detailed content of Why does `mysqli_query()` throw 'Warning: mysqli_query() expects parameter 1 to be mysqli' error?. For more information, please follow other related articles on the PHP Chinese website!