while 循环不显示从数据库收集的所有数据

while loop not showing all the data gathered from database

while 循环不显示从数据库收集的所有数据.. 仅显示页面中最后收集的项目。而不是全部显示。

<?php 
    include "connect_to_mysql.php"; 
    $dynamicList = "";
    $sql = mysql_query("SELECT * FROM product ORDER BY date DESC LIMIT 6");
    $productCount = mysql_num_rows($sql); // count the output amount
    if ($productCount > 0) {

    //here is problem in this while loop.. dont know why it is not gathering all the items.


       while($row = mysql_fetch_array($sql)){ 
         $id = $row["id"];
         $product_name = $row["p_name"];
         $price = $row["price"];
         $date_added = strftime("%b %d, %Y", strtotime($row["date"]));
         $details = $row["details"];
         $category = $row["category"];
         $subcategory = $row["sub_category"];
         $category2 = $row["category2"];
         $img_name = $row["img_name"];
         $v_id = $row["v_id"];
         $v_name = $row["v_name"];
         $v_number = $row["v_number"];
         $v_email = $row["v_email"];

         $dynamicList="

           <div id=\"single_product\" style=\"float:left; margin-left:20px; padding:10px;\">

           <h3> $product_name  </h3>
           <h3> <img src='pics/$img_name' width='200px' height='200px'/> </h3>
           <p><b><center> RUP $price </center></b></p>

           <a href=\"details.php\" style=\"float:left; font-size:20px;\">Details</a>
           <a href=\"cart.php\" style=\"float:right; font-size:20px;\">Add To Cart</a>

           </div>            
         ";

       }
   } else {
        $dynamicList = "We have no products listed in our store yet";
   }
   mysql_close();
?>

我被困在这里了,请把我从这里救出来

尝试删除您的:

 $productCount = mysql_num_rows($sql); // count the output amount

您已经在整个循环中提取行并在循环为空时给出 else 语句。你不一定要数它们。

几个月前我遇到了类似的事情,我相信这与您现在遇到的问题相同。

将 if 语句粘贴到 while 循环中:

if ($productCount > 0) {

然后放在while循环上面:

$productCount = 0;

并且在 while 循环中,在您的 if 语句之前:

$productCount++;

编辑:

我刚刚注意到其他事情,您实际上并没有回显您的动态列表。您正在设置变量但不显示它?

您正在使用

$dynamicList="...

在你的循环中。我假设您稍后会在代码中回应 $dynamicList 。您需要使用文本连接而不仅仅是 =

也一样

$dynamicList .= "...

它会在每次循环时添加到字符串而不是覆盖它