gpt4 book ai didi

php - JSON 为空,而数据库不为空

转载 作者:行者123 更新时间:2023-11-29 21:11:23 27 4
gpt4 key购买 nike

我正在尝试使用 localhost 从数据库中使用 php 创建 json 文件。对于一个表“voteofthanks”,其工作绝对正常,但为另一个表编写类似的代码,生成的 json 文件为空 -> results.json。 Table for which it is working is:

创建其 json 的代码是:

<?php 
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "StageReady");
$myConnection = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD, DB_DATABASE) or die ("could not connect to mysql");
$sqlCommand="SELECT id, content FROM voteofthanks ";
$posts = array();
$result=mysqli_query($myConnection, $sqlCommand) or die(mysql_error());
while($row=mysqli_fetch_array($result))
{
$title=$row['id'];
$url=$row['content'];
$posts[] = array('id'=> $title, 'content'=> $url);

}

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($posts));
fclose($fp);
?>

并且 localhost/JSON/results.json 显示正确

table "englishquotes" is here

现在对于表 englishquotes 我编写相同的代码

<?php 
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "StageReady");
$myConnection = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD, DB_DATABASE) or die ("could not connect to mysql");
$sqlCommand="SELECT id, content FROM englishquotes ";
$posts = array();
$result=mysqli_query($myConnection, $sqlCommand) or die(mysql_error());
while($row=mysqli_fetch_array($result))
{
$title=$row['id'];
$url=$row['content'];

$posts[] = array('id'=> $title, 'content'=> $url);

}

$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($posts));
fclose($fp);

?>

我在打开 localhost/english/results.json 时得到一个空白的白页

最佳答案

尝试在该行中使用htmlspecialchars()

$posts[] = array('id'=> htmlspecialchars($title), 'content'=> htmlspecialchars($url));

关于php - JSON 为空,而数据库不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36335989/

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