Encoding PHP MySQL Query Response to JSON Issue
<p>I am basically trying to call a MySQL Query and get the response from the Server, which I have done and it works. Now I need to encode the response as a JSON Array (Each Row as a JSON Object and add them to a JSON Array)</p>

<p><strong>My Code:</strong></p>

<pre><code>foreach ($db-&gt;query('SELECT * from mydb.UserTable') as $sqlresp) {
        $rows = array();
        while($r = mysqli_fetch_assoc($sqlresp)) {
              $rows['users'][] = $r;
        }
        print json_encode($rows);
}
</code></pre>

<p><strong>The above code gives me this error:</strong></p>

<blockquote>
  <p>Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result,
  array</p>
</blockquote>

<p><strong>It points to this line:</strong></p>

<pre><code>while($r = mysqli_fetch_assoc($sqlresp)) {
</code></pre>

<p><strong>JSON Object I need in the JSON Array:</strong></p>

<p>Roughly this would be the structure..</p>

<pre><code>user
{
     name: "john",
     picture: "http://...."
     details {
          email: "email@gmail.com",
          telephone: "3456..."
     }
}
</code></pre>

<p><strong>*Column names of the table are the same as the Object Property names (e.g.: name, picture, etc)</strong></p>

<p>What's wrong with my code and how do I properly encode my query response's rows as a JSON Array, folks?</p>

<p><strong>EDIT:</strong></p>

<p><strong><em>I need the Array because I need to send this array to a Javascript function. So I need it in JSON Array Format</em></strong></p>