mysqli_free_result()期望参数1为mysqli_result,null中给出null

时间:2015-12-30 01:17:11

标签: php mysql mysqli sql-insert

我刚刚将网站从一个域迁移到另一个域(以及另一个主机)。我确保所有链接都被替换并导出/导入了我的数据库。迁移似乎有效,但由于某种原因,我在我的新域上收到了一个错误,我没有在我的旧域上获取(据我所知,相同的代码)。

我的错误是:“警告:mysqli_free_result()要求参数1为mysqli_result,在第84行的路径中给出null”。我查看了解决此错误的其他StackOverflow问题,但我还没有找到解决方案。

这是我的代码:

<?php
session_start();

// 1. Create a database connection
$dbhost = 
$dbuser = 
$dbpass = 
$dbname = 
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection occurred.
if(mysqli_connect_errno()) {
  die("Database connection failed: " . 
    mysqli_connect_error() .
    " (" . mysqli_connect_errno() . ")"
    );
}

// 2. Perform database query
if (empty($_SESSION['order'])) {
    $query = "INSERT INTO `orders` (`order_id`) VALUES (NULL)";
    $result = mysqli_query($connection, $query);
    // Test if there was a query error
    if (!$result) {
    die("Database query failed.");
    }
    // 3. Use returned data (if any)
    $order_id_recent = mysqli_insert_id($connection);
    $_SESSION['order'] = $order_id_recent;
}

$size = $_POST["size"];
$paper = $_POST["paper"];
$type = $_POST["type"];
$quantity = $_POST["quantity"];

// 2. Perform database query
$query2 = "SELECT product_id FROM product WHERE product_type = '$type' AND size = '$size' AND paper = '$paper'";
$result2 = mysqli_query($connection, $query2);
// Test if there was a query error
if (!$result2) {
  die("Database query failed.");
}

// 3. Use returned data (if any)
while($row = mysqli_fetch_assoc($result2)) {
    $product_id = $row['product_id'];
}

$order_id = $_SESSION['order'];

// 2. Perform database query
$order_id = $_SESSION['order'];

$query3 = "SELECT * FROM order_item WHERE order_id = '$order_id' AND product_id = '$product_id'";
$result3 = mysqli_query($connection, $query3);
// Test if there was a query error
if (!$result3) {
  die("Database query failed.");
}

while($row = mysqli_fetch_assoc($result3)) { 
    $itemexistrows = mysqli_num_rows($result3);
}

if ($itemexistrows > 0) {
    $query4 = "UPDATE order_item SET quantity = quantity + '$quantity' WHERE product_id = '$product_id' AND order_id = '$order_id'";
    $result4 = mysqli_query($connection, $query4);
    if (!$result4) {
    die("Database query failed.");
    } else {
        echo 'The item has been added to your cart. <a class="text-red" href="viewcart.php">View your cart</a></div>.'; 
    }
} else {
    $query5 = "INSERT INTO `order_item`(`order_item_id`, `product_id`, `quantity`,`order_id`) VALUES (NULL,'$product_id','$quantity','$order_id')";
    $result5 = mysqli_query($connection, $query5);
    if (!$result5) {
    die("Database query failed.");
    } else {
        echo 'The item has been added to your cart. <a class="text-red" href="viewcart.php">View your cart</a></div>.'; 
    }
}

// 4. Release returned data
mysqli_free_result($result);

// 5. Close database connection
mysqli_close($connection);
?>

奇怪的是,我的网站似乎仍然有效。这些代码行是购物车模块的一部分,购物车似乎得到了更新。

我希望有人可以帮助我。

谢谢,

文森特

1 个答案:

答案 0 :(得分:1)

我怀疑$_SESSION['order']不是空的,因此$ result没有设置,因为它似乎没有在同一个if语句中解决。

以下几乎肯定会使问题消失。

 if(isset($result) && $result!=null){
     // 4. Release returned data
     mysqli_free_result($result);
 }

或者,正如评论中所建议的那样,可能更好:

 if(isset($result) && is_resource($result)){
     // 4. Release returned data
     mysqli_free_result($result);
 }