Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

php - Is there any memory limit for json_encode() method?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Stab into the dark: some of your database rows contain non-ASCII characters (e.g. ü, é and such). Your database connection is set to latin1, so the data is not UTF-8 encoded. json_encode requires UTF-8 encoded data. If you fetch enough rows, there will be rows with such non-UTF-8 data in there, and json_encode fails. With few enough rows you happen to not hit those problematic rows.

Test this by outputting echo json_last_error_msg(); after json_encode.

Set your database connection to UTF-8. See here how to do so: UTF-8 all the way through

The reason why your browser complains about invalid JSON when you include a print_r is simple: because then PHP outputs a lot of garbage which isn't JSON, which the browser can't decode as JSON.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...