我试图回应一个由数组组成的json编码数组,但我不知道它不会让我打印那个东西.这是我的代码:
include_once('confi.php');
header('Content-type: application/json');
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
$lastRecord = isset($_POST['lastRecordID']) ?
mysql_real_escape_string($_POST['lastRecordID']) : "";
$queryForTotalRec = mysql_query("SELECT customer_id FROM `WebServiceTesting`.`sapphire` ORDER BY customer_id DESC LIMIT 1");
$total_rec = mysql_fetch_row($queryForTotalRec);
if($total_rec){
$queryForAllRecords = "SELECT * FROM `WebServiceTesting`.`sapphire` WHERE customer_ID BETWEEN %d AND %d";
$get_all_recs = mysql_query(sprintf($queryForAllRecords, $lastRecord, $total_rec[0]));
$json = array();
while($row = mysql_fetch_assoc($get_all_recs)){
$json[] = array("Status" => 1, "NewRecord" => $row);
}
print_r($json);
echo json_encode($json);
}else{
$json = array("status" => 0, "Error_Message" => mysql_error());
echo json_encode($json);
}
}else{
$json = array("status" => 0, "Error_Message" => "Request Method not correct");
echo json_encode($json);
}
@mysql_close($conn);
错误:
格式错误的JSON:意外的’A’有时’我’
当我删除print_r行时,我得到:
没有收到回复
当我打印$json数组的计数iam得到153但没有其他输出.
我试过的事情:
我读了一些解决类似问题的解决方案,你需要使用array_values()
例如:
echo json_encode(array_values($json));
同样的回答:’没有收到回复’
我也尝试将echo $json放在循环中,我知道这在概念上是错误的,但仍然得到预期的错误’语法错误’
此外,我试图回应通过foreach没有运气语法错误,但我可以看到原始输出但无法验证json.
只是对于print_r的信息,这是响应:
Array (
[0] => Array (
[Status] => 1 [NewRecord] => Array (
[customer_id] => 1241
[firstName] => Katy
[lastName] => Lest
[email] => klest@yahoo.com [phone] => 787012425
)
)
[1] => Array (
[Status] => 1 [NewRecord] => Array (
[customer_id] => 1242
[firstName] => Hanah
[lastName] => Morrisn
[email] => road@gmail.com
[phone] => 144221275 )
)
[2] => Array (
[Status] => 1 [NewRecord] => Array (
[customer_id] => 1243
[firstName] => James
[lastName] => McGrath
[email] => rosamcgrath@hotmail.com
[phone] => 79684312 )
)
)
刚刚找到了一个答案,我仍然在寻找一个理由,如果有人可以提供帮助.我拉的唱片数量是150,所以我一次尝试了50条记录并且效果很好.任何人都知道我怎样才能真正为我的阵列分配更多内存,以便它只能一次性保存所有必需的数据?
我也试过给出准确的索引以及我认为数组内存不足但这甚至不起作用:
$json = new SplFixedArray($difference);
非常感谢您的协助.