0

这是我的代码

<? 
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"}]

任何人都可以帮助找出错误或必须做些什么才能在最后得到方括号

4

4 回答 4

0

这段代码确实应该输出[{...},...]。所以我们不能真正告诉你你那边出了什么问题。$response在转换为 Json 之前检查变量的结构,看看出了什么问题。

请注意,该代码允许 SQL 注入。您必须更改它,以便在 SQL 查询中对参数$amp和进行转义。$row['phone1']即使您现在依赖魔术 qoutes,此解决方案也不是面向未来的(现在确实是现在证明的),因为在 PHP 5.4 中已删除了对此的支持。

于 2013-09-12T12:31:55.153 回答
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"}]
于 2013-09-12T12:33:02.577 回答
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"}]
于 2013-09-12T12:27:50.677 回答
0

你写的应该工作:

http://ideone.com/ErV9fr

// 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","re​​pairStatus":"$repairStatus","savedAt":"$savedAt"},{"response_number":1,"ackNo":"dffdgd", "repairStatus":"$repairStatus","savedAt":"$savedAt"},{"response_number":2,"ackNo":"dffdgd","re​​pairStatus":"$repairStatus","savedAt":"$savedAt "}]

除此之外,您应该清理 $amp 变量。在当前的形式中,用户逃避您的查询并对您的数据库执行任意查询将是微不足道的。

http://www.php.net/manual/en/mysqli.real-escape-string.php

于 2013-09-12T12:49:36.320 回答