gpt4 book ai didi

php - mysqli_free_result() 期望参数 1 为 mysqli_result,null 给定

转载 作者:行者123 更新时间:2023-11-29 02:52:13 24 4
gpt4 key购买 nike

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

我的错误是:“警告:mysqli_free_result() 期望参数 1 为 mysqli_result,在第 84 行的 path 中给定为 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);
?>

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

最佳答案

我怀疑 $_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);
}

关于php - mysqli_free_result() 期望参数 1 为 mysqli_result,null 给定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34521594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com