Home >Database >Mysql Tutorial >Why am I getting the 'Warning: mysqli_query() expects parameter 1 to be mysqli, resource given' error?
mysqli_query() Warning: Mixing MySQL and MySQLi Extensions
The error you encounter, "Warning: mysqli_query() expects parameter 1 to be mysqli, resource given," indicates a mismatch between the syntax used for mysqli_query() and the type of connection object you are passing to it.
In your code, you have declared a connection using the mysql_connect() function, which belongs to the deprecated mysql extension. However, you are trying to use the mysqli_query() function, which works with the mysqli extension.
To resolve this issue, you need to consistently use either mysql or mysqli extensions throughout your code. It is recommended to use mysqli, as it offers improved functionality and security over the mysql extension.
Here are the necessary changes:
// Use mysqli instead of mysql for the connection $myConnection = mysqli_connect("$db_host", "$db_username", "$db_pass") or die ("could not connect to mysql"); // Use mysqli_select_db instead of mysql_select_db for database selection mysqli_select_db($myConnection, "mrmagicadam") or die ("no database");
Additionally, you can replace all instances of mysql_fetch_array() with mysqli_fetch_array($query). This change ensures that you are using the correct function for retrieving data from the mysqli result object.
The above is the detailed content of Why am I getting the 'Warning: mysqli_query() expects parameter 1 to be mysqli, resource given' error?. For more information, please follow other related articles on the PHP Chinese website!