HTMLタグを含むテーブルに一部のデータを公開しようとしています。ページにアクセスすると、HTMLを含まないフィールドはすべて正常ですが、HTMLを含むフィールドはすべてnullです。htmlタグを含むときにnullを返すJSONフィールド
フィールドをVarCharとTextに設定しようとしましたが、どちらもうまくいかないようです。テーブルは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関数を使用するhtmlを含む$ rowにhttp://php.net/manual/fr/function.htmlentities.phpを使用すると、json_encodeのオプションを使用して完了できます。詳細については、http://php.net/を参照してください。マニュアル/ ja/function.json-encode.php – Fky
私はhtmlエンティティを実装し、フィールドをnullから空に変更しました。私はjson_encodeオプションを使って何をすべきか100%ではないのですか? – geolaw