按列使用php mysql组结果?

时间:2012-06-13 03:58:12

标签: php

我有一张包含数据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

如何解决?

2 个答案:

答案 0 :(得分:1)

mysql_fetch_object返回一个对象,因此在foreach中迭代它不会做任何事情。

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

答案 1 :(得分: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>