1

So, does anyone knew there's limitation php to encode array using json_encode ?

I fetch from SQLServer using below code and it was succesful until the last 2 lines of json_encode,

    while($row = sqlsrv_fetch_array($stmt,  SQLSRV_FETCH_ASSOC))        
    {
       array_push($items, $row);        
    }

    $result = array();
    $result["data"] = $items;

    echo count($items);
    echo "<br/>";
    echo count($result);
    echo "<br/>";
    echo count($result["data"]);
    echo "<br/>";

    header('Content-Type: application/json');
    echo json_encode($result, 128); 

I have search to the phpmanual, and although not mentioning anything about increased the memory, my settings at php.ini is set to 2GB, so I am sure that's not the case.

When I count the array before encode, they return the rows just fine.

Thus, I did little test, increase the memory at php.ini and use SELECT TOP in the query, found out that it can return 6670 data rows using SELECT TOP (6670) *,

but ...

not one record show after I changed it to 6671 or just SELECT * which supposedly return about 13522 data rows, while I need them all?

  • you can't change content-type after using echo –  Oct 30 '15 at 04:25
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) –  Oct 30 '15 at 04:26
  • "supposedly" - make sure – sectus Oct 30 '15 at 04:31
  • 1
    It looks like you want to return 2 different responses (HTML and JSON) to the same request which is not possible in HTTP protocol. The problem you are facing is indeed described in answers suggested by @Terminus, but it is unlikely to help you as you probably should be doing something different altogether (i.e. rendering inline JavaScript). Clarifying your actual goal may help someone to answer your question. – Alexei Levenkov Oct 30 '15 at 05:36
  • 1
    @AlexeiLevenkov what's echoing out is just the counts of the array in different ways. It appears to be debugging code. If he wants that info on his page he simply needs to do a `.length` on the `JSON.parse ()`d responseText. The solution is removing the debug code. –  Oct 30 '15 at 05:40
  • Not that you're wrong about the impossibility of having 2 different responses in the same request, +1 –  Oct 30 '15 at 05:42

0 Answers0