MySQL+;JSON:检索id=term的所有数据
我试图从数据库中检索JSON数据,并在表中解析它们 目前我检索它的方式如下:MySQL+;JSON:检索id=term的所有数据,mysql,json,html-table,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
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();
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;
<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);
这将给我一个结果,不管有多少行id为“term”
我的mysql.php文件如下所示:
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();
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;
<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);
以下是我的数据库的外观:
因此这个术语将是carid,我想要包含carid这个术语的行中的所有值。然后,单行上的每个值都在一个值之间。大概是这样的:
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();
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;
<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);
轮胎
10-10-2012
31-10-2012
乔治
8951235
0
乱数假文
灯
17-10-2012
23-10-2012
安东尼
4367234
0
乱数假文
您的while
循环每次都覆盖相同的数组项,而不是生成二维数组。应该是:
$json = array();
while ($row = mysql_fetch_array($result)) {
$json[] = $row;
}
echo json_encode($json);
for (var i = 0; i < json.length; i++) {
console.log(json[i].service);
}
在JavaScript中,需要循环返回的数组。而不是console.log(json.service)
它应该是:
$json = array();
while ($row = mysql_fetch_array($result)) {
$json[] = $row;
}
echo json_encode($json);
for (var i = 0; i < json.length; i++) {
console.log(json[i].service);
}
for(var i=0;i
试着这样做:
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();
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;
<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);
我仍然只得到一个值,即lights。您的JavaScript必须在数组上循环,请参阅我编辑的答案。我很惊讶你在控制台上记录了任何东西。在这种情况下,两者都可以工作。有关差异的解释,请参阅。我没有得到任何结果。我不确定我做错了什么。检索数据的方法正确吗?您在该PHP页面上设置了JSON头了吗?