请帮助:警告:mysqli_num_rows()期望参数1为mysqli_result,给定对象

时间:2017-10-16 21:59:39

标签: mysql

我试图用这个带有mysql的动态表创建一个网站

我尝试了所有内容,无法解决此代码的问题。

请帮助

<?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
$dynamicList = "";
mysqli_query($con,"SELECT * FROM products ORDER BY date_added DESC LIMIT 6");
$productCount = mysqli_num_rows($con); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($con)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
        <tr>
          <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></a></td>
          <td width="83%" valign="top">' . $product_name . '<br />
            $' . $price . '<br />
            <a href="product.php?id=' . $id . '">View Product Details</a></td>
        </tr>
      </table>';
    }
} else {
    $dynamicList = "We have no products listed in our store yet";
}
mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:0)

您需要使用mysqli_query()的结果作为mysqli_num_rows()的参数,而不是连接。

$result = mysqli_query($con,"SELECT * FROM products ORDER BY date_added DESC LIMIT 6");
$productCount = mysqli_num_rows($result);

同样适用于mysqli_fetch_array(),它应该是

while($row = mysqli_fetch_array($result)){