2014-03-26 33 views
0
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; 
} 

這是我的職責。不能從SQL查詢getted元素添加到陣列

我用它從其他文件

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

回答

1

更新您的代碼,要覆蓋你的$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']; 
} 
+0

而($行= mysql_fetch_array(-----我的失誤---- $結果------)){ – user2022879

+0

是的,有一個的錯誤 –