Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON为空,而数据库不为空_Php_Mysql_Json - Fatal编程技术网

Php JSON为空,而数据库不为空

Php JSON为空,而数据库不为空,php,mysql,json,Php,Mysql,Json,我正在尝试使用php从使用localhost的数据库创建json文件。对于一个表“VoteOfTanks”,它的工作非常好,但为另一个表编写了类似的代码,结果json文件为空->results.json。 创建其json的代码是: <?php define("DB_HOST", "localhost"); define("DB_USER", "root"); define("DB_PASSWORD", ""); define("DB_DATABASE", "St

我正在尝试使用php从使用localhost的数据库创建json文件。对于一个表“VoteOfTanks”,它的工作非常好,但为另一个表编写了类似的代码,结果json文件为空->results.json。

创建其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显示正确

现在,对于表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));

空白页面=>和
mysql\u error()
不会与
mysqli\u混用。所以请执行
mysqli\u error($myConnection)
,这就是为什么您没有得到真正的错误。感谢您纠正我@Fred ii非常感谢@jqhearts,但现在我得到了一些空的数据字段和一些填充的数据字段!