我想在包含HTML標記的表中公開一些數據,當我訪問頁面時,所有不包含HTML的字段都可以,但任何包含html的返回null。當包含html標記時,返回null的JSON字段
我曾嘗試將字段設置爲VarChar和文本,但都沒有工作。該表格也設置爲utf8_general_ci
。
request_step_content.php
<?php
header("Content-Type:application/json");
header("Access-Control-Allow-Origin: *");
$user = "userName";
$pass = "userPassword";
$table = "myTable";
$db=new PDO("mysql:host=localhost;dbname=$table", $user, $pass);
$query = "SELECT * FROM steps";
try {
$stmt = $db->query($query);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error.";
die(json_encode($response));
}
$rows = $stmt->fetchAll();
if($rows) {
$response["success"] = 1;
$response["message"] = "Step";
$response["step"] = array();
foreach ($rows as $row) {
$post = array();
$post["id"] = $row["intID"];
$post["heading"] = $row["strStepheading"];
$post["keyPrinciple"] = $row["strKeyPrinciple"];
$post["pillar"] = $row["strPillar"];
$post["introduction"] = $row["memIntroduction"];
$post["actionSteps"] = $row["memActionSteps"];
$post["actionPoints"] = $row["memActionPoints"];
$post["studyAndUnderstanding"] = $row["memStudyUnderstanding"];
array_push($response["step"], $post);
}
echo json_encode($response);
}
else {
$response["success"] = 0;
$response["message"] = "No Steps Available";
die(json_encode($response));
}
?>
JSON響應
{"success":1,
"message":
"Step",
"step":[{"id":"1",
"heading":"Test Heading",
"keyPrinciple":"Key Principle: Test Key Principle",
"pillar":"Pillar 1: Pillar",
"introduction":null,
"actionSteps":null,
"actionPoints":null,
"studyAndUnderstanding":null}]}
使用htmlentities函數http://php.net/manual/fr/function.htmlentities.php對包含html的$行,您可以使用json_encode上的選項來完成查看http://php.net/的選項部分詳細信息manual/en/function.json-encode.php – Fky
我實現了html實體,並將字段從null更改爲空。我不是100%的我應該用json_encode選項做什麼? – geolaw