Php can';t将获取的元素从sql查询添加到数组
这是我的职责 我从其他文件中使用它Php can';t将获取的元素从sql查询添加到数组,php,mysql,sql,Php,Mysql,Sql,这是我的职责 我从其他文件中使用它 function get_comment_count_for_events() { $query = "SELECT event_token , COUNT(NULLIF(event_token, '')) AS counts FROM comment GROUP BY event_token ORDER BY counts DESC;"; $result = mysql_query($query); $comment_count =
function get_comment_count_for_events() {
$query = "SELECT event_token , COUNT(NULLIF(event_token, '')) AS counts FROM comment GROUP BY event_token ORDER BY counts DESC;";
$result = mysql_query($query);
$comment_count = array();
while ($row = mysql_fetch_array($query)) {
$comment_count = $row;
}
if (!$result) {
trigger_error('Invalid query: ' . mysql_error() . " in " . $query);
}
return $comment_count;
}
但在Database中,当我测试查询时,它的工作是:
结果:
事件标记-计数
1-13
2-13
8-11
3-8
5-7
7-4
6-3
'-0更新您的代码,您正在覆盖您的
$comment\u count
变量。您需要改用数组
foreach (get_comment_count_for_events() as $comment_count_event) {
echo $comment_count_event['tiken_event'];
echo $comment_count_event['count'];
}
在第二次迭代中,字段名也是不正确的。也要更新它们
while ($row = mysql_fetch_array($result)) {
$comment_count[] = $row;
}
而($row=mysql\u fetch\u数组(-----我的错误----$result------){
foreach (get_comment_count_for_events() as $comment_count_event) {
echo $comment_count_event['event_token'];
echo $comment_count_event['counts'];
}