php – Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given-ThrowExceptions

Exception or error:

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);
How to solve:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *