2012-02-18 47 views
2

我有下面的PHP代码:再回到陌生的JSON阵列从MySQL

$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,authorNamenoteBody - 我不确定它在哪里与"0","1","2"

回答

1

尝试要么mysql_fetch_assoc或mysql_fetch_object在

+0

是的mysql_fetch_assoc做到了..谢谢:) – Snowman 2012-02-18 17:37:31

2

mysql_fetch_array()这是默认的“模式”取得的结果作为关联&数字数组。所以你得到字段名称(你想要的)和数字索引(你不想要的数字)。

要解决这个问题,请将常量“MYSQL_ASSOC”作为第二个参数传递给“mysql_fetch_array”或使用mysql_fetch_assoc()函数。