这是我的代码
<?
include '../dbConnect.php';
$amp=trim($_POST['amp']);
//$amp='AMP8';
//$sql=mysql_query("select tblRepairQueue.ackNo,tblRepairQueue.repairStatus,tblRepairQueue.savedAt from tblRepairQueue,AMPcustomers where AMPcustomers.phone1=tblRepairQueue.phoneNo and AMPcustomers.id='".$amp."'");
$sql=mysql_query("select phone1 from AMPcustomers where id='".$amp."'");
$response = array();
while($row=mysql_fetch_array($sql))
{
$sql_query=mysql_query("select ackNo,repairStatus,savedAt from tblRepairQueue where phoneNo='".$row['phone1']."'");
while($row1=mysql_fetch_array($sql_query)){
$ackNo=$row1['ackNo'];
$repairStatus=$row1['repairStatus'];
$savedAt=$row1['savedAt'];
$response[]=array('ackNo'=>$ackNo,'repairStatus'=>$repairStatus,'savedAt'=>$savedAt);
}}
print json_encode($response);
?>
输出m得到
{"ackNo":"26101211236759","repairStatus":"Closed and Complete","savedAt":"2012-10-26 00:55:25",{"ackNo":"031212102614381","repairStatus":"Closed and Complete","savedAt":"2012-12-02 23:05:54"}
但我希望输出看起来像
[{"ackNo":"26101211236759","repairStatus":"Closed and Complete","savedAt":"2012-10-26 00:55:25"},{"ackNo":"031212102614381","repairStatus":"Closed and Complete","savedAt":"2012-12-02 23:05:54"}]
任何人都可以帮助找到错误或必须采取什么措施才能在最后找到方括号
答案 0 :(得分:0)
这有点奇怪,因为我有这段代码:
<?php
$array = array();
$array[] = array("ackNo"=>"26101211236759","repairStatus"=>"Closed and Complete","savedAt"=>"2012-10-26 00:55:25");
$array[] = array("ackNo"=>"26101211236780","repairStatus"=>"Closed and Complete","savedAt"=>"2012-10-26 10:55:25");
echo json_encode($array);
?>
我得到了正确的表格:
[{"ackNo":"26101211236759","repairStatus":"Closed and Complete","savedAt":"2012-10-26 00:55:25"},{"ackNo":"26101211236780","repairStatus":"Closed and Complete","savedAt":"2012-10-26 10:55:25"}]
答案 1 :(得分:0)
此代码确实应输出[{...},...]
。因此,我们无法告诉您身边出了什么问题。在转换为Json之前检查$response
变量的结构,看看出了什么问题。
请注意,该代码允许SQL注入。您必须对其进行更改,以便在SQL查询中转义参数$amp
和$row['phone1']
。即使你现在依赖于魔术,这个解决方案也不是面向未来的(现在可以证明),因为在PHP 5.4中删除了对此的支持。
答案 2 :(得分:0)
请重新检查它无法提供类似{"ackNo":"26101211236759","repairStatus":"Closed and Complete","savedAt":"2012-10-26 00:55:25",{"ackNo":"031212102614381","repairStatus":"Closed and Complete","savedAt":"2012-12-02 23:05:54"}
因为它正在创建一个数组数组,所以它不能像那样打印。
它将始终打印为
[{"ackNo":"26101211236759","repairStatus":"Closed and Complete","savedAt":"2012-10-26 00:55:25"},{"ackNo":"031212102614381","repairStatus":"Closed and Complete","savedAt":"2012-12-02 23:05:54"}]
答案 3 :(得分:0)
你所写的内容应该有效:
// How many to add
$response_count=3;
// Your response, just templated
$response_template=array(
'response_number'=>0,
'ackNo'=>'dffdgd',
'repairStatus'=>'$repairStatus',
'savedAt'=>'$savedAt'
);
// Your empty response array
$response = array();
for($i=0;$i<$response_count;$i++) {
$response_template['response_number'] = $i; // Set the 'response number' to the itteration.
$response[]= $response_template; // Add the template to the collection
}
print json_encode($response);
结果:
[{ “response_number”:0 “ackNo”: “dffdgd”, “repairStatus”: “$ repairStatus”, “savedAt”: “$ savedAt”},{ “response_number”:1, “ackNo”:” dffdgd”, “repairStatus”: “$ repairStatus”, “savedAt”: “$ savedAt”},{ “response_number”:2, “ackNo”: “dffdgd”, “repairStatus”: “$ repairStatus”, “savedAt”: “$ savedAt”}]
除此之外,您应该清理$ amp变量。在它的当前形式中,用户可以逃避查询并对数据库执行任意查询,这将是微不足道的。