Php 从MySQL取回奇怪的JSON数组
我有以下PHP代码:Php 从MySQL取回奇怪的JSON数组,php,mysql,json,Php,Mysql,Json,我有以下PHP代码: $data=mysql_query("SELECT * FROM notes WHERE rootNoteId='$noteId'"); $mainArray; while($result = mysql_fetch_array($data)) { $mainArray[]=$result; } $sendback = array( "mainArray" => $mainArray ); sendResponse(200, json_encode
$data=mysql_query("SELECT * FROM notes WHERE rootNoteId='$noteId'");
$mainArray;
while($result = mysql_fetch_array($data))
{
$mainArray[]=$result;
}
$sendback = array(
"mainArray" => $mainArray
);
sendResponse(200, json_encode($sendback));
我的表“备注”包含以下字段:
'noteId'
'authorName'
'noteBody'
但是,我的返回JSON字符串具有以下格式:
{
"0": "3",
"1": "Moe Bit",
"2": "Sub sub ",
"noteId": "3",
"authorName": "Moe Bit",
"noteBody": "Sub sub "
}
为什么要为具有重复表字段值的数组添加
0,1,2
索引?我只想要noteId
、authorName
和noteBody
——我不确定它在哪里出现了“0”、“1”、“2”
尝试mysql\u fetch\u assoc或mysql\u fetch\u object在默认的“模式”中以关联和数字数组的形式获取结果。因此,您可以得到字段名(您想要的)和数字索引(您不想要的数字)
要解决此问题,请将常量“MYSQL\u ASSOC”作为第二个参数传递给“MYSQL\u fetch\u array”或使用函数