I'm playing with a guestbook/chatroom idea and have a MySQL table setup with three columns:
1 - id - Primary Key auto increment
2 - name - String
3 - comment - string
I have very little experience with PHP but this is what I've put together for this operation:
$result = mysql_query("SELECT * FROM guestbook");
$i = 0;
while($row = mysql_fetch_array($result))
{
//add the row to the $chat array at specific index of $i
$chat[$i] = $row;
$i += 1;
}
$encode = json_encode($chat);
echo "$encode";
However, output from this looks pretty awful:
[{"0":"1","id":"1","1":"Justin ","name":"Justin ","2":"Comment 1","comment":"Comment 1"},
{"0":"2","id":"2","1":"Justin ","name":"Justin ","2":"Another comment","comment":"Another comment"},
{"0":"3","id":"3","1":"Justin ","name":"Justin ","2":"Look at this comment!","comment":"Look at this comment!"},
{"0":"4","id":"4","1":"Justin ","name":"Justin ","2":"Ok I'm done talking","comment":"Ok I'm done talking"}]
I was hoping to get three fields: id, name, and comment, but it looks like things doubled. Can anyone help?
Thanks!