2012-02-07 33 views
3

我試圖將從數據庫檢索到的數據保存到.json中。這是剛剛嘗試過的。如何從MySQL php查詢創建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 

還有什麼問題呢?

+0

你有寫權限嗎? – 2012-02-07 17:52:45

+0

[Warning:mysql_fetch_ *期望參數1是資源,布爾給定錯誤]的可能的重複(http://stackoverflow.com/questions/11674312/warning-mysql-fetch-expects-parameter-1-to-be-resource -boolean-given-error) – 2012-08-02 17:41:13

回答

2

該文件夾/Applications/XAMPP/xamppfiles/htdocs/test不能被Apache寫入 - 更改權限以便寫入。在Windows資源管理器中,右鍵點擊test文件夾,並取消「只讀」。

+1

謝謝,完美的工作。 – user1190466 2012-02-07 18:02:49