如何显示count()查询中的数据?

时间:2013-10-24 15:26:19

标签: php mysql

我正在运行此查询并获得结果:

|  COUNT(DISTINCT member_extension)  |  queue_name        |  queue_account_id
|  21                                |     Standard       |  1105
|  16                                |     Premium        |  1133
|  21                                |     WorldMark      |  1202
|  8                                 |     Escalation     |  1248
|  17                                |     Wingate Queue  |  1249

但是,当我尝试将它们放在表中时,我似乎无法显示member_extension COUNT()总数。我做错了什么?

我收到此错误:

  

注意:未定义的索引:第59行的C:\ Apache24 \ htdocs \ phone-dashboard \ testing \ count_test.php中的member_extension

$sth = $conn->prepare("SELECT COUNT(DISTINCT member_extension),     queue_name,`queue_account_id`
FROM CallLog
WHERE type = 'completed'
BETWEEN '2013-10-22 09:00:00'
AND '2013-10-24 09:00:00'
GROUP BY queue_account_id
"); 
$sth->execute();
$sql = $sth->fetchAll(PDO::FETCH_ASSOC);

echo '<table>';
echo "<tr><td>" . 'Queue Name' . "</td><td>" . 'Queue Account ID' . "</td><td>" . 'Queue Total' . "</td></tr>";
try {   
    foreach ($sql as $rs) {
        echo '<tr>';
        echo '<td>' . $rs['queue_name'] . '</td>';
        echo '<td>' . $rs['queue_account_id'] . '</td>';
        echo '<td>' . $rs['member_extension'] . '</td>';
        echo '</tr>';
        }

    echo '</table>';
    }
catch (PDOException $e) {
print $e->getMessage();
}
?>

1 个答案:

答案 0 :(得分:3)

为计数提供一个您可以轻松参考的别名:

SELECT COUNT(DISTINCT member_extension) AS num_extensions


foreach ($sql as $rs) {
    echo '<tr>';
    echo '<td>' . $rs['queue_name'] . '</td>';
    echo '<td>' . $rs['queue_account_id'] . '</td>';
    echo '<td>' . $rs['num_extensions'] . '</td>';
    echo '</tr>';
    }