title: How to encode large amount of data as json
body: <p>I need to be able to pass a json from php to the client so javascript can parse the code and return it. Currently, the way I have always done this is:</p>
  
  <pre><code>&lt;?php
      $mysql_query = $mysqli-&gt;query(QUERY GOES HERE);
      $array - array();
      while($row = $mysql_query-&gt;fetch_assoc()){
          array_push($array, $row);
      }
      $json =json_encode($array);
  ?&gt;
  &lt;!-- javascript --&gt;
  &lt;script&gt;
      var json = &lt;?php echo $json;?&gt;;
      //...
  &lt;/script&gt;
  &lt;!--- rest of html ---&gt;
  </code></pre>
  
  <p>This usually works. However, the query returns more than 100,000 rows, and php is currently running out of memory on creating the entire array. I have seen some people say to use ajax. Is this the only way? And if so, how exactly would I go about implementing it? Or is there a more efficient method of encoding the mysql data into json without ajax?</p>
  
  <p>Thank you </p>