PHP, Mysql avg while loop to JSON
<p>I am having an issue understanding the proper use for a while loop in PHP. I am trying to get a JSON output like this ....</p>

<pre><code>{"ratings":[{"id":"1","rating":"4.5"}},{"id":"2","rating":"3"}}]}
</code></pre>

<p>My php looks like this ...</p>

<pre><code>&lt;?php
$db_name="ratings";
require_once ('db_Connect.php');
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT id, AVG(rating) FROM ratings GROUP BY id"; 

$result = mysql_query($sql) or die(mysql_error());
$result = mysql_fetch_array($result);

echo json_encode($result);
mysql_close($con);
?&gt;
</code></pre>

<p>What the above php produces is ...</p>

<pre><code>{"0":"1","id":"1","1":"1.5","AVG(rating)":"1.5"}
</code></pre>

<p>however, there are more results that should be displayed. And as you can see, this is not the anticipated results as there are only one row shown. And the format is not as desired. </p>

<p>My question is, where have I gone wrong with my code? </p>