MySQL + JSON : Retrieve all data where id = term

标签 mysql json html-table

我正在尝试使用 JSON 从我的数据库中检索数据并在表中解析它们。

目前我是这样检索的:

xmlhttp = new XMLHttpRequest()

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

        console.log(json.service)

    }
}

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

这将给我一个结果,无论我有多少行 ID 为“term”。

我的 mysql.php 文件如下所示:

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);

mysql_close();

break;

这是我的数据库的样子:

enter image description here

所以术语将是 cardid,我想要包含 carid 术语的行中的所有值。然后单行上的每个值都介于 a 之间。像这样:

<tbody>
    <tr>
        <td>Tyre</td>
        <td>10-10-2012</td>
        <td>31-10-2012</td>
        <td></td>
        <td>George</td>
        <td>8951235</td>
        <td>0</td>
        <td>Lorem Ipsum</td>
    </tr>
    <tr>
        <td>Lights</td>
        <td>17-10-2012</td>
        <td>23-10-2012</td>
        <td></td>
        <td>Antony</td>
        <td>4367234</td>
        <td>0</td>
        <td>Lorem Ipsum</td>
    </tr>
</tbody>

最佳答案

像这样尝试:

    $temp=0;
    $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'];
    $temp++;
        }
print json_encode($json);

关于MySQL + JSON : Retrieve all data where id = term,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12985421/

相关文章:

javascript - 根据用户输入在 Highcharts 中添加系列

Android:解析JSON中的特殊字符(ä,ö,ü)

c# - 获取类型 'where' 上的泛型方法 'System.Linq.Queryable' 与提供的类型参数和参数不兼容

java - 如何在 wicket 中嵌套动态表?

MySQL查询以列出2个用户之间的共同值

MySql SELECT,根据另一列对一列求和

mysql - 扩展 MySQL 数据库,提高许多连接的性能

mysql - 将列值从一个表复制到另一个匹配的 ID

html-table - thead 中的两行,无法在第二个中设置列宽

html - 你能用 HTML Tables 模仿 div 的右浮动吗?