Home >Backend Development >PHP Tutorial >GROUP BY 语句如何用php输出来类

GROUP BY 语句如何用php输出来类

WBOY
WBOYOriginal
2016-06-13 11:54:021055browse

GROUP BY 语句怎么用php输出来类
数据库 tabel 中有 `chengji` 字段 内容为 及格 不及格 良好 三种值
页面显示出三种值的统计数
我用

SELECT `chengji`,count(*) FROM `table` GROUP BY `chengji`

在Mysql 中可以出来 可是要怎么显示到php文件中那

页面样子为
         | 不及格 |   及格  |  良好  |
数量 |              |             |            |


------解决方案--------------------
$sql = SELECT `chengji`,count(*)  AS count FROM `table` GROUP BY `chengji`
在php中把mysql_query($sql);的结果打印出来看看
------解决方案--------------------
$sql = SELECT `chengji`,count(*)  AS count FROM `table` GROUP BY `chengji`;<br />$rs = mysql_query($sql);<br />while($row = mysql_fetch_assoc($rs)) {<br />  $res[$row['chengji']] = $row['count'];<br />}<br />print_r($res);<br />
可以得到类似这样的数组
Array<br />(<br />    [及格] => 4<br />    [不及格] => 1<br />    [良好] => 3<br />)<br />

然后
$res[''] = '数量';<br />$head = array('', '不及格', '及格', '良好');<br />echo '<table>';<br />echo '<tr><th>' . join('</th><th>', $head) . '</th></tr>';<br />echo '<tr>';<br />foreach($head as $v) echo '<td>' . (isset($res[$v]) ? $res[$v] : '') . '</td>';<br />echo '</tr></table>';<br />

------解决方案--------------------

引用:
再问个小问题 怎么用date() 获取 当月  月初 和 月末 日期


<br /><?php<br />date_default_timezone_set("PRC");<br /><br />echo "当前月份是".date("m")."\n";<br />echo "本月初日期是".date("m")."-01\n";<br />echo "本月未日期是".date("m")."-".date("t")."\n";<br />?><br />
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn