PHP json_encode SOMETIMES 返回 "null"作为数据库查询的值

标签 php mysql mysqli

从数据库进行 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/

相关文章:

php - mysql 受 View 限制但按字母顺序排列?

php - mysql对多列唯一

sql - 如何从集合的角度考虑聚合函数

php - 连接到本地主机出现错误

php - 如何在 PHP 中同时创建表格、更改列并向其中插入数据?

php - undefined variable 和mysqli错误

php - 使用ios上传图片到php

php - 当买家在 OpenCart 下订单时,更新产品 'quantity' 的代码是什么?

mysql - MySQL 响应中结果数量不同

php - ZF2 文件上传合集