gpt4 book ai didi

php - JSON 字段包含 html 标签时返回 null

转载 作者:行者123 更新时间:2023-11-29 19:45:49 25 4
gpt4 key购买 nike

我试图在包含 HTML 标签的表中公开一些数据,当我访问该页面时,所有不包含 HTML 的字段都很好,但任何包含 html 的字段都会返回 null。

我尝试将字段设置为 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}]}

最佳答案

问题出在编码上。 DB 表中的列可以是 UTF-8,但存储的数据不必是 UTF-8。 PHP 函数 json_encode 仅接受 UTF-8 数据。检查您存储的数据库数据。

关于php - JSON 字段包含 html 标签时返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40992590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com