0

我有一张数据表 mobile(id, name, size)

(1, "Asha 301", "240 x 400");
(2, "Asha 302", "480 x 800");
(3, "Asha 303", "240 x 400");
(4, "Asha 304", "480 x 800");
(5, "Asha 305", "240 x 400");
(6, "Asha 306", "240 x 400");
<?php
    $query = "SELECT id, name, size
              FROM `mobile`
              GROUP BY size Order by display_size ";
    $results = mysql_fetch_array($query);
   foreach ($results as $result) {
        ?>
        <li>
           <?php echo $result['size']; ?>
           <ul>
               <li><?php echo $result['name']; ?></li>
           </ul>
        </li>
<?php
}
?>

结果是:

240 x 400
   Asha 301
400 x 800
   Asha 302

结果只得到 1 行,如何获取所有行?

240 x 400
   Asha 301
   Asha 303
   Asha 305
   Asha 306
400 x 800
   Asha 302
   Asha 304

如何解决?

4

2 回答 2

1
 $query = "SELECT `id`, `name`, `size` FROM `mobile` Order by `size` ";
 $results = mysql_query($query);
 while ($result=mysql_fetch_array($results)) {
  ?>
    <li>
       <?php echo $result['size']; ?>
       <ul>
           <li><?php echo $result['name']; ?></li>
       </ul>
    </li>
于 2012-06-13T05:01:44.797 回答
1

mysql_fetch_object返回一个对象,因此在 foreach 中迭代它什么也不做。

相反,您应该使用mysql_fetch_array,它返回一个结果数组,

于 2012-06-13T04:01:06.570 回答