title: How do I properly use PHP to encode MySQL object into JSON?
body: <p>I am trying to iterate through a MySQL object and use an ajax call on another page to append the data but I can't get the php to return valid JSON to the callback.</p>
  
  <p>This one obviously doesn't work...</p>
  
  <pre><code>&lt;?php
  
      $db_host = "localhost";
      $db_user = "blah";
      $db_pass = "blah";
      $db_name = "chat";
      $mysqli = new MySQLi($db_host, $db_user, $db_pass, $db_name);
      $myQuery = "SELECT * FROM users";
      $result = $mysqli-&gt;query($myQuery) or die($mysqli-&gt;error);
      $row = $result-&gt;fetch_assoc();
      echo json_encode($row);
  
  ?&gt;
  </code></pre>
  
  <p>Or this one...</p>
  
  <pre><code>&lt;?php
  
      $db_host = "localhost";
      $db_user = "blah";
      $db_pass = "blah";
      $db_name = "chat";
      $mysqli = new MySQLi($db_host, $db_user, $db_pass, $db_name);
      $myQuery = "SELECT * FROM users";
      $result = $mysqli-&gt;query($myQuery) or die($mysqli-&gt;error);
      while ( $row = $result-&gt;fetch_assoc() ){
          echo json_encode($row) . ", ";
      }
  
  ?&gt;
  </code></pre>