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

时间:2016-07-11 00:50:01

标签: 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>";

其他领域都很好。

我很感激任何帮助!感谢

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。 我的解决方案是将数据库中的文本字段属性从utf8_general_ci更改为asci_bin。

问候