我正在尝试使用JSON w / PHP从MySQL数据库中检索数据并遇到问题。我从服务器返回表,除非我在查询中包含“注释”字段。我检查了JSON LInt中的JSON,然后回来了。 MySQL查询自行检出。在firebug中,我看到了SyntaxError:JSON.parse:JSON数据的第1行第184列的字符串文字中的错误控制字符。我只是在页面上得到我的标题。代码是: 使用example.php
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "*****", "*****", "inventory_form");
$result = $conn->query("SELECT Comments, FName, LName, Eqpmnt_Brwd, Date_Taken, Brwd_Rsn, Service_Tag FROM Inventory");
$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "[") {$outp .= ",";}
$outp .= '{"Comments":"' . $rs["Comments"] . '",';
$outp .= '"FirstName":"' . $rs["FName"] . '",';
$outp .= '"Eqpmnt_Brwd":"'. $rs["Eqpmnt_Brwd"] . '",';
$outp .= '"Date_Taken":"'. $rs["Date_Taken"] . '",';
$outp .= '"Brwd_Rsn":"'. $rs["Brwd_Rsn"] . '",';
$outp .= '"ServiceTag":"'. $rs["Service_Tag"] . '"}';
}
$outp .="]";
$conn->close();
echo($outp);
?>
的index.html:
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
border-bottom: 3px solid #cc9900;
color: #996600;
font-size: 30px;
}
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
</head>
<body>
<h1>SHS Inventory Form</h1>
<div id="id01"></div>
<script>
var xmlhttp = new XMLHttpRequest();
var url = "inventory_table.php";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<table>";
for(i = 0; i < arr.length; i++) {
out += "<tr><td>" +
arr[i].Comments +
"</td><td>" +
arr[i].FirstName +
"</td><td>" +
arr[i].Eqpmnt_Brwd +
"</td><td>" +
arr[i].Date_Taken +
"</td><td>" +
arr[i].Brwd_Rsn +
"</td><td>" +
arr[i].ServiceTag +
"</td></tr>";
}
out += "</table>"
document.getElementById("id01").innerHTML = out;
}
</script>
</body>
</html>
并且响应文本的示例是:
"[{"Comments":"","FirstName":"Nadine","Eqpmnt_Brwd":"Apple Video Dongle","Date_Taken":"2014-09-05","Brwd_Rsn":"Returned","ServiceTag":""},{"Comments":"Wants to check out hovercam. Can retrieve it if anyone needs a hovercam.","FirstName":"Nicole ","Eqpmnt_Brwd":"Hovercam","Date_Taken":"2014-09-04","Brwd_Rsn":"Borrowed","ServiceTag":"075642"},{"Comments":"with SD card All six cameras borrowed on 9/8/14 will be used throughout that school week.Expected return date is Monday 9/15/14","FirstName":"George","Eqpmnt_Brwd":"Nikon D3100 Camera","Date_Taken":"2014-09-08","Brwd_Rsn":"Borrowed","ServiceTag":"074753"},{"Comments":"w/ SD card "
“评论”字段中的某些文字是否存在问题需要转义?我看过网上但很难找到很多话题。意识到你应该使用PDO,但是好运找到例子。一旦我让这个例子正常工作,我将继续努力。
答案 0 :(得分:1)
正如@Barmar所说,使用json_encode
而不是试图手工构建JSON字符串。例如:
$data = array();
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
$data[] = $rs;
}
$conn->close();
echo json_encode($data);
回复您对PDO的评论/问题...使用PDO,您可以使用PDOStatement::fetchAll
简化此操作:
$pdo = new PDO($dsn, $user, $pass);
$stmt = $pdo->query("SELECT Comments, FName, LName, Eqpmnt_Brwd, Date_Taken, Brwd_Rsn, Service_Tag FROM Inventory");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);