0

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)." }";   

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I got the echo $query to display something, and then it was easy to display the $mailbox.
But one question though (it is related), in my testing I made the LogCat act as a backup display, so that I should be seeing the same thing coming back to the emulator and on the php page. But only the LogCat displays the mailbox's contents, and the php page still says mailbox array is empty?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.