I am trying to echo a json-encoded array which consist of an array but i dont know it is not letting me print that thing. Here's my code:
<?php
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);
Errors:
Malformed JSON: Unexpected 'A' sometimes 'I'
When i am deleting the print_r line iam getting:
No response received
When i am printing the count of $json array iam getting a count of 153 but NO OTHER output.
Things i've tried:
i read in some solutions to similar problems that u need to use array_values()
for e.g:
echo json_encode(array_values($json));
same response: 'No response received'
I've also tried putting echo $json inside loop which I know that is conceptually wrong but still and got expected error 'Syntax error'
Also, i tried echoing through foreach no luck Syntax error but i can see output in raw but cannot validate the json.
Just for the info on print_r this is the response:
Array (
[0] => Array (
[Status] => 1 [NewRecord] => Array (
[customer_id] => 1241
[firstName] => Katy
[lastName] => Lest
[email] => [email protected] [phone] => 787012425
)
)
[1] => Array (
[Status] => 1 [NewRecord] => Array (
[customer_id] => 1242
[firstName] => Hanah
[lastName] => Morrisn
[email] => [email protected]
[phone] => 144221275 )
)
[2] => Array (
[Status] => 1 [NewRecord] => Array (
[customer_id] => 1243
[firstName] => James
[lastName] => McGrath
[email] => [email protected]
[phone] => 79684312 )
)
)
Just found a sort of answer to this i am still looking for a reason if anyone can help in that please. The number of Records i was pulling were 150+ so i just tried with 50 records at a time and it worked perfectly. Anyone know how can i actually allocate more memory to my array so that it can hold all the required data at once only ?
I have also tried by giving accurate index as well i thought that array goes out of memory but this even not working:
$json = new SplFixedArray($difference);
Your assistance would be very much appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…