Why am I getting this error message:
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given
My code is:
$statement = "INSERT INTO table1 (data1, data2) VALUES ('$variable1', '$variable2')";
if ($result = mysqli_query($conn,$statement)) {
echo "New record added successfully";
} else {
echo "Error adding records: " . $result . "<br>" . mysqli_error($conn);
}
echo "Adding records finished. ";
mysqli_free_result($result);
Warning: mysqli_query () expects parameter 1 to be mysqli, resource given in C:\xampp\htdocs\limitless\connect_to_mysql.php on line 17 What I am doing wrong? Show activity on this post. You are mixing mysqli and mysql extensions, which will not work.
You are using improper syntax. If you read the docs mysqli_query () you will find that it needs two parameter. mysql $link generally means, the resource object of the established mysqli connection to query the database.
So there is not even no need to call mysqli_free_result () but it is wrong because you (should) know that $result can't be a mysqli_result. The original source on which your code is based was probably executing a SQL commmand that will return a mysqli_result. But even then your code would be wrong because it should be only freed upon success.
If the code you have used as base also calls mysqli_free_result () outside a conditional block as in your posted code, it is even worse: You have copied code doing it wrong. If you need example code for PHP database operations, refer to the official documentation.
As stated in the mysqli_query manual:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
Your insert query will return true or false, but not an object. So, calling mysqli_free_result
will not work here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With