警告:mysql_num_rows():提供的参数不是有效的 MySQL 结果资源

新手上路,请多包涵

我在下面的代码中收到以下错误消息(在查询末尾):

警告: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 许可协议

阅读 394
2 个回答

改变 $result = @mysql_query ($query);

$result = mysql_query ($query) or die(mysql_error());

看看你是否有任何错误。

编辑:

您在 oc.price 之后和 prd.products_id 之前漏掉了一个逗号。像这样更改您的查询:

 $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/*here*/,/**/
             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
         ";

原文由 CristiC 发布,翻译遵循 CC BY-SA 3.0 许可协议

你的sql可能有错误,

显示 如下

 if (!mysql_query($query, $link)) {
    echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
}

原文由 Haim Evgi 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏