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

This is my function.

I use it from other file

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

But in databese when i test query it's work: result: event_token - counts

1 - 13

2 - 13

8 - 11

3 - 8

5 - 7

7 - 4

6 - 3

''- 0

1 Answer 1

1

Update your code, you are overriding your $comment_count variable. You need to use array instead;

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

Also in your second iteration ,field names are incorrect. Update them also;

foreach (get_comment_count_for_events() as $comment_count_event) {
    echo $comment_count_event['event_token'];
    echo $comment_count_event['counts'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

while ($row = mysql_fetch_array(-----my mistake----$result------)) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.