从数据库进行 SELECT 查询时,有时,我会在文本字段中得到一个空值。这似乎是随机的,但通常在我更新数据库后,这个问题似乎会随之而来。以下是我的代码:
require_once('database.php');
$sql = "SELECT stories.id, authors.name AS author, authors.dates AS dates, authors.life AS life, stories.title AS title, stories.text AS text, stories.summary AS summary, stories.reading_time AS time, stories.cost AS cost, stories.file_name AS story_filename, category.name AS category
FROM stories
INNER JOIN category ON stories.category_id = category.id
INNER JOIN authors ON stories.author_id = authors.id";
if ($result = mysqli_query($con, $sql)) {
$resultArray = array();
$tempArray = array();
while($row = $result->fetch_object()) {
$tempArray = $row;
array_push($resultArray, $tempArray);
}
echo json_encode($resultArray, JSON_PRETTY_PRINT);
}
mysqli_close($con);
错误发生在文本字段上。它作为
text = "<null>";
其他字段没问题。
我很感激这方面的任何帮助!谢谢
最佳答案
我遇到了同样的问题。 我的解决方案是将数据库中的文本字段属性从 utf8_general_ci 更改为 asci_bin。
问候
关于PHP json_encode SOMETIMES 返回 "null"作为数据库查询的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38297750/