I am trying to input data into a grid which has CRUD operations its called Jquery JTable, but there seems to be some sort of error with the Json conversion from the MYSQL table to JSON, for the data to be displayed on the table it needs to look kinda like this:
{
"Result":"OK",
"Records":[
{"PersonId":1,"Name":"Benjamin Button","Age":17,"RecordDate":"\/Date(1320259705710)\/"},
{"PersonId":2,"Name":"Douglas Adams","Age":42,"RecordDate":"\/Date(1320259705710)\/"},
{"PersonId":3,"Name":"Isaac Asimov","Age":26,"RecordDate":"\/Date(1320259705710)\/"},
{"PersonId":4,"Name":"Thomas More","Age":65,"RecordDate":"\/Date(1320259705710)\/"}
]
}
But whenever I implement the json_encode function in php it gives me this:
{
"Result":"OK",
"Records":[
{"0":"1","PersonId":"1","1":"Benjamin Button","Name":"Benjamin Button","2":"17","Age":"17","3":"2011-12-27 00:00:00","RecordDate":"2011-12-27 00:00:00"},
{"0":"2","PersonId":"2","1":"Douglas Adams","Name":"Douglas Adams","2":"42","Age":"42","3":"2011-12-26 00:00:00","RecordDate":"2011-12-26 00:00:00"},
{"0":"3","PersonId":"3","1":"Isaac Asimov","Name":"Isaac Asimov","2":"26","Age":"26","3":"2011-12-28 00:00:00","RecordDate":"2011-12-28 00:00:00"},
{"0":"4","PersonId":"4","1":"Thomas More","Name":"Thomas More","2":"61","Age":"61","3":"2011-12-27 00:00:00","RecordDate":"2011-12-27 00:00:00"},
{"0":"5","PersonId":"5","1":"Ihsan Oktay Anar","Name":"Ihsan Oktay Anar","2":"44","Age":"44","3":"2012-01-03 20:55:02","RecordDate":"2012-01-03 20:55:02"}
]
}
Here is my PHP code:
<?php
//Get records from database
$result = mysqli_query($conn, "SELECT * FROM people;");
//Add all records to an array
$rows = array();
while($row = mysqli_fetch_array($result))
{
$rows[] = $row;
}
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['Records'] = $rows;
print json_encode($jTableResult);
?>
the table is called people and it has four fields: personId, Name, Age and RecordDate.
Any ideas would be greatly appreciated.
mysqli_fetch_arraywhich returns index keys (0/1/2/etc...), you want to usemysqli_fetch_assoc()OR simply change it to this:mysqli_fetch_array($result, MYSQLI_ASSOC)$rows = mysqli_fetch_array($result);and use$rows;