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

2022-01-20 00:00:00 fetch php mysql

可能重复:
警告:mysql_fetch_array():提供的参数不是有效的 MySQL 结果

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

I am getting the following error message on the code below (which is at the end of the query):

警告:mysql_num_rows():已提供参数不是有效的 MySQL 结果../view-ind-order.php 中的资源第 28 行

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ../view-ind-order.php on line 28

该脚本应该检索订单(从列出订单表中所有 order_id 行的页面)、订单内容、订购用户和产品信息.我认为我得到错误的地方是订单中有不止一种产品,但我不太清楚我哪里出错了.(头部有会话启动命令)

This script is supposed to retrieve the order (from a page which lists all of the order_id rows from the orders table), the contents of the order, the user who ordered and the product information. I think where I'm getting the error is where there is more than one product within the order but I can't quite see where I'm going wrong. (the header has a session start command)

     <?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');
?>

推荐答案

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

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

看看你是否有任何错误.

and see if you have any errors.

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

You missed a comma after oc.price and before prd.products_id. Change your query like this:

$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    
         ";

相关文章