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

GROUP BY 语句怎么用php输出来类

WBOY
Release: 2016-06-23 13:59:32
Original
1050 people have browsed it

数据库 tabel 中有 `chengji` 字段 内容为 及格 不及格 良好 三种值
页面显示出三种值的统计数
我用

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

在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`
在php中把mysql_query($sql);的结果打印出来看看

结果是Resource id #5

$sql = SELECT `chengji`,count(*) AS count FROM `table` GROUP BY `chengji`
在php中把mysql_query($sql);的结果打印出来看看

用mysql_fetch_array

结果为Array ( [0] => 及格 [type] => 及格 [1] => 4 [count] => 4 )

用mysql_fetch_array
结果为Array ( [0] => 及格 [type] => 及格 [1] => 4 [count] => 4 )
while($row = mysql_fetch_array()){}


用mysql_fetch_array
结果为Array ( [0] => 及格 [type] => 及格 [1] => 4 [count] => 4 )
while($row = mysql_fetch_array()){}
我明白了 这样和数据库中查询出的格式的一样的
可是我想在页面中做一个固定的table 和数据库中的格式不一样

$sql = SELECT `chengji`,count(*)  AS count FROM `table` GROUP BY `chengji`;$rs = mysql_query($sql);while($row = mysql_fetch_assoc($rs)) {  $res[$row['chengji']] = $row['count'];}print_r($res);
Copy after login
可以得到类似这样的数组
Array(    [及格] => 4    [不及格] => 1    [良好] => 3)
Copy after login

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

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

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

<?phpdate_default_timezone_set("PRC");echo "当前月份是".date("m")."\n";echo "本月初日期是".date("m")."-01\n";echo "本月未日期是".date("m")."-".date("t")."\n";?>
Copy after login

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template