我在很多地方都在使用我的 MySQL 数据库中用 PHP 编码的 JSON 对象,没有任何问题。我现在正在尝试在语法中使用 COUNT(column) 从结果集中创建一个,我认为它导致了问题,但我不知道如何解决它。
我的JS看起来像这样......
createTeacherStatsTab = function(){
$('#main').append('<div id="teacherStatsTab></div>"');
$.getJSON("php/countMarkingPerTeacher.php", function(data) {
$.each(data, function(key, val) {
$('#teacherStatsTab').append(val.teacher + ' : ' + val.count(teacher));
});
});
}
我的PHP看起来像这样......
$result = mysql_query("
SELECT teacher, COUNT(teacher)
FROM Assessment
GROUP BY teacher
ORDER BY COUNT(teacher) DESC
");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
回来的数据是这样的……
[
{
teacher: "SDe",
COUNT(teacher): "413"
},
{
teacher: "OJe",
COUNT(teacher): "297"
},
{
teacher: "AMi",
COUNT(teacher): "257"
},
{
teacher: "ASt",
COUNT(teacher): "218"
},
{
teacher: "VJa",
COUNT(teacher): "194"
},
{
teacher: "SLa",
COUNT(teacher): "125"
},
{
teacher: "MCr",
COUNT(teacher): "99"
}
]
感觉这可能与 GROUP BY 剥离 ID 或其他什么有关?
有人可以帮忙吗?
谢谢!