I’m trying to generate a json file with PHP with the code below and I’m getting an empty array -> {“posts”:[]}. I’m using WordPress. Can someone assist me. Thanks
$sql=mysql_query("SELECT * FROM wp_posts");
$response = array();
$posts = array();
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
$url['url']=$row;
$title['title']=$row;
$posts[] = array('url'=> $url, 'title'=> $title);
}
$response['posts'] = $posts;
$fp = fopen('results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
There are many mistakes in your code! Please check this:
Try this
Reference: visit here
Problem is in your while loop. Try this: