在PHP中使用JSON返回多个数组

I am trying to return 2 arrays along with a third variable by encoding all of them into a JSON array. I want access to all the keys of both the arrays and also the third variable. I have had a hard time trying to do so. All i was able to do was this, without any success..

Help required. Is there a way I can copy the whole json_encoded data['states'] into a JS array?

onlineStudents.php

<?php
require_once 'myfunctions.php';
$query="Select * from online_students";
$result=  queryMysql($query);
$data["total"]=mysql_num_rows($result)-1;
$query="Select distinct state from online_students";
$result=queryMysql($query);
while($row=  mysql_fetch_array($result))
{
    $query2="select * from online_students where state='$row[state]'";
    $result2=queryMysql($query2);
    $data["states"]=mysql_num_rows($result2);
}
$query="Select distinct college from online_students";
$result=  queryMysql($query);
while($row=  mysql_fetch_array($result))
{
    $query2="Select * from online_students where college='$row[college]'";
    $result2=queryMysql($query2);
    $data["colleges"]=mysql_num_rows($result2);
}
echo json_encode($data);
?>

myfunctions.php

function queryMysql($query)
{
    $result=mysql_query($query) or die(mysql_error());
    return $result;
}

myfunctions.js

function peoples()
{
 $.getJSON("onlineStudents.php",function(data){
        $("#chat_head_number").html(": "+data['total']);
        $("#chat_states_number").html(data['states']);  //i want the whole array data[states] instead of a single value
        $("#chat_college_number").html(data['colleges']);
        });
}
douxian6086
douxian6086 -我没有意识到这一点。谢谢你的更新。
大约 7 年之前 回复
dpa84373
dpa84373 -但即使没有大括号,查询运行正常。
大约 7 年之前 回复
douni9620
douni9620 toolong
大约 7 年之前 回复
doumi1852
doumi1852 offtopic:请注意,不推荐使用PHP中的mysql_xxx()函数,不鼓励使用它们。您应该考虑切换到较新的数据库API,例如PDO库。
大约 7 年之前 回复
dououde4065
dououde4065 你的JSON发送和阅读似乎很好。您可能必须将{}围绕$row[state]放在SQL查询中,如{$row[state]}。你检查过那些被正确更换了吗?
大约 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐