簡體   English   中英

無法從SQL查詢中將獲取的元素添加到數組

[英]can't add getted elements from sql query to array

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

這是我的職責

我從其他文件使用

foreach (get_comment_count_for_events() as $comment_count_event) {
    echo $comment_count_event['tiken_event'];
    echo $comment_count_event['count'];
}

但是在databese中,當我測試查詢時它是有效的:結果:event_token-計數

1-13

2-13

8-11

3-8

5-7

7-4

6-3

''-0

更新代碼,您將覆蓋$comment_count變量。 您需要使用數組來代替;

while ($row = mysql_fetch_array($result)) {
    $comment_count[] = $row;
}

同樣在第二次迭代中,字段名稱不正確。 還更新它們;

foreach (get_comment_count_for_events() as $comment_count_event) {
    echo $comment_count_event['event_token'];
    echo $comment_count_event['counts'];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM