Reputation: 95
How do I fix the following code to echo the JSON encoded array of particular rows in the SQL database table? Because all I get right now is: { "mailbox":[] }
Here is my code:
$query = "SELECT * FROM messages WHERE touser='$from'";
$result = mysql_query($query)
or die("\n\ndatabase error!\n". mysql_error());
$mailbox = array();
while($row = mysql_fetch_assoc($result)){
$mailbox[] = $row;
}
echo "{ \"mailbox\":".json_encode($mailbox)." }";
Upvotes: 0
Views: 458
Reputation: 19905
Are you sure $mailbox
array contains anything? My bet is that your mysql query returns an empty resultset.
Insert a echo "Got a row: \n"; print_r($row);
inside the while loop to check this.
If that gives nothing then your SQL is wrong or your tables contains no data. To see the actual query, insert echo "Query: '$query'\n"
. Now run the query manually without involving php.
Upvotes: 1