require_once("conect.php");
$sqlString="SELECT * FROM articles;";
$response = array();
$posts = array();
$query=mysql_query($sqlString) or die (mysql_error());
while ($row=mysql_fetch_array($query)){
$title =$row["title"];
$author =$row["author"];
$article =$row["article"];
$posts[] = array('title'=> $title, 'author'=> $author, 'article'=> $article);
}
$response['posts'] = $posts;
$fp = fopen('json\results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);`
结果...
{"posts":[{"title":"tatoo","author":"lakmal","article":"A tattoo is a ."},
{"title":"dog","author":"lakmal","article":"The domestic dog"},
{"title":"cat","author":"chamikara","article":"The domestic"},
{"title":"Automobile","author":"lakmal","article":"An automobile"}]}
我希望它保存为 jsonobject