如何从mysql php查询创建json文件

时间:2012-02-07 17:46:24

标签: php mysql json

我正在尝试将从数据库中检索到的数据保存到.json中。这就是刚刚尝试过的。

$sql = mysql_query("SELECT `positive`,`time` FROM sentiment WHERE acctid=1");


$response = array();
$posts = array();
while($row=mysql_fetch_array($sql)) 
{ 

$positive=$row['positive']; 

$time=$row['time']; 


$posts[] = array('positive'=> $positive, 'time'=> $time,);

} 

$response['posts'] = $posts;

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

我收到了以下错误:

Warning: fopen(results.json) [function.fopen]: failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/test/getjson.php on line 29

Warning: fwrite() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/test/getjson.php on line 30

Warning: fclose() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/test/getjson.php on line 31

问题是什么?

1 个答案:

答案 0 :(得分:2)

Apache无法写入文件夹/Applications/XAMPP/xamppfiles/htdocs/test - 更改权限以便它可以写入。在Windows资源管理器中,右键单击test文件夹,然后取消选中“只读”。