Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php can';t将获取的元素从sql查询添加到数组_Php_Mysql_Sql - Fatal编程技术网

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'];
}