jQuerybeast jQuerybeast - 1 year ago 44
JSON Question

MySQL + JSON: Retrieve all data where id = term

I am trying to retrieve data with JSON from my database and parse them in a table.

Currently I retrieve it like this:

xmlhttp = new XMLHttpRequest()

xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsontext = xmlhttp.responseText;
var json = JSON.parse(jsontext);



xmlhttp.open("GET", "mysql.php?p=getservice" + "&carid=" + "KYF111", true);

This will give me one result, no matter how many rows I have with id 'term'.

My mysql.php file looks like this:

case 'getservice':

$q = mysql_real_escape_string($_GET['q']);
$carid = stripslashes($_GET['carid']);
$query = "SELECT * FROM Service WHERE carid = '".$carid."'";

$result = mysql_query($query);

$json = array();
while ($row = mysql_fetch_array($result)) {

$json['carid'] = $row['carid'];
$json['service'] = $row['service'];
$json['date'] = $row['date'];
$json['nextdate'] = $row['nextdate'];
$json['kilometers'] = $row['kilometers'];
$json['servicedby'] = $row['servicedby'];
$json['invoice'] = $row['invoice'];
$json['cost'] = $row['cost'];
$json['remarks'] = $row['remarks'];

print json_encode($json);



This is how my database looks like:

enter image description here

so the term would be the cardid, and I want all the values from the rows which contains carid the term. Then each value on a single row gets between a . Something like this:

<td>Lorem Ipsum</td>
<td>Lorem Ipsum</td>

Answer Source

Try like this:

    $json = array();
        while ($row = mysql_fetch_array($result)) {

            $json[$temp]['carid'] = $row['carid'];
            $json[$temp]['service'] = $row['service'];
            $json[$temp]['date'] = $row['date'];
            $json[$temp]['nextdate'] = $row['nextdate'];
            $json[$temp]['kilometers'] = $row['kilometers'];
            $json[$temp]['servicedby'] = $row['servicedby'];
            $json[$temp]['invoice'] = $row['invoice'];
            $json[$temp]['cost'] = $row['cost'];
            $json[$temp]['remarks'] = $row['remarks'];
print json_encode($json);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download