我在下面的代码中收到以下错误消息(在查询末尾):
警告:mysql_num_rows():提供的参数不是第 28 行 ../view-ind-order.php 中的有效 MySQL 结果资源
该脚本应该检索订单(从列出订单表中所有 order_id 行的页面)、订单内容、订购的用户和产品信息。我认为我遇到错误的地方是订单中有不止一种产品,但我不太明白哪里出错了。 (标题有一个会话启动命令)
<?php
$page_title = 'Order';
include ('./includes/header.html');
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) )
{
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) )
{
$id = $_POST['id'];
} else {
echo 'This page has been accessed in error';
include ('./includes/header.html');
exit();
}
require_once ('mydatabase.php');
$query = "SELECT us.users_id, us.users_first_name, us.users_surname, us.users_business,
ord.order_id, ord.users_id, ord.total, ord.order_date,
oc.oc_id, oc.order_id, oc.products_id, oc.quantity, oc.price
prd.products_id, prd.products_name, prd.price
FROM users AS us, orders AS ord, order_contents AS oc, products AS prd
WHERE ord.order_id=$id
AND us.users_id = ord.users_id
AND ord.order_id = oc.order_id
AND oc.products_id = prd.products_id
";
$result = @mysql_query ($query);
if (mysql_num_rows($result) == 1) {
$row = mysql_fetch_array ($result, MYSQL_NUM);
echo '
<table>
<tr>
<td><strong>Name:</strong></td>
<td>' . $row[1] . ' ' . $row[2] . '</td>
</tr>
<tr>
<td><strong>Business Name</strong></td>
<td>' . $row[4] . '</td>
</tr>
<tr>
<td><strong>Total:</strong></td>
<td>' . $row[7] . '</td>
</tr>
<tr>
<td><strong>Quantity</strong></td>
<td>' . $row[12] . '</td>
</tr>
<tr>
<td><strong>Product:</strong></td>
<td>' . $row[15] . '</td>
</tr>
<tr>
<td><strong>Price:</strong></td>
<td>' . $row[13] . '</td>
</tr>
</table>
';
} else {
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}
mysql_close();
include ('./includes/footer.html');
?>
原文由 AdamMc 发布,翻译遵循 CC BY-SA 4.0 许可协议
改变
$result = @mysql_query ($query);
和
看看你是否有任何错误。
编辑:
您在 oc.price 之后和 prd.products_id 之前漏掉了一个逗号。像这样更改您的查询: