I am executing a simple ajax> php> mysql example posted here http://openenergymonitor.org/emon/node/107
I can only display information from the first line. My table is set up like this
-------------- | id | name| -------------- | 1 | Pat | | 2 | Joe | | 3 | Rob | --------------
Php code
$result = mysql_query("SELECT * FROM $tableName"); //query $array = mysql_fetch_row($result); //fetch result echo json_encode($array);
script
$(function () { $.ajax({ url: 'api.php', data: "", dataType: 'json', success: function(data) { var id = data[0];
Row 1
If I put var id = data[0]; , I will get the value 1. If I put var name = data[1]; I get Pat.
ROWS 2 n 3 undefined
Example var id=data[2]; returns undefined, etc.
My questions
From the other questions on Stackoverflow, I see that I probably have to use a while loop, but I'm not sure why and how.
Tryharder
source share