我刚刚将一个站点从一个域迁移到另一个域(和另一个主机)。我确保所有链接都被替换并导出/导入了我的数据库。迁移似乎成功了,但由于某种原因,我在新域上遇到了一个错误,而我没有在旧域上得到这个错误(据我所知,是用相同的代码)。
我的错误是:“警告:mysqli_free_result()期望参数1是mysqli_result,在第84行的路径中为空”。我查看了解决此错误的其他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/