Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given [duplicate]

Tags:

php

mysqli

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);
like image 620
Tass Mark Avatar asked Jul 22 '15 08:07

Tass Mark


People also ask

What does mysqli_query () expect parameter 1 to be?

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.

Why is mysqli_query () not working?

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.

Is it necessary to call mysqli_free_result ()?

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.

Why is mysqli_free_result () not working outside a conditional block?

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.


1 Answers

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.

like image 77
Burki Avatar answered Oct 06 '22 00:10

Burki