警告:mysqli_free_result() 期望参数 1 是 mysqli_result,给定的布尔值

2021-12-25 00:00:00 php mysqli

为什么我会收到此错误消息:

Why am I getting this error message:

警告:mysqli_free_result() 期望参数 1 是 mysqli_result,给出布尔值

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

我的代码是:

$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);

推荐答案

如中所述mysqli_query 手册:

失败时返回 FALSE.对于成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查询,mysqli_query() 将返回一个 mysqli_result 对象.对于其他成功的查询,mysqli_query() 将返回 TRUE.

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.

您的插入查询将返回 true 或 false,但不会返回对象.因此,调用 mysqli_free_result 在这里不起作用.

Your insert query will return true or false, but not an object. So, calling mysqli_free_result will not work here.

相关文章