php - WordPress to .json file....no data appearing -
try convert couple of wordpress table rows wp_posts .json file. not json api plugin i've seen posted on many times. i've tried , produces data. want id, title , post or page content. that's it. also, json api doesn't export .json file. want on server dynamically.
so i'm using script produce .json file.
<?php $con=mysql_connect("localhost","username","password") or die("database connection failed"); if($con) { mysql_selectdb("database_wp",$con); } ?> <?php $data=array(); $qa=array(); $query=mysql_query("select id, title, content wp_posts order id"); while($geteach=mysql_fetch_array($query)) { $id=$getdata[0]; $title=$getdata[1]; $content=$getdata[2]; $qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content); } $data['qa']=$qa; $fp = fopen('myjson.json', 'w'); fwrite($fp, json_encode($data)); fclose($fp); ?>
but in resulting .json file {"qa":[]}
, that's it. there data on server. why can't pull in? what's fix?
i think problem in while loop.
you use
while($geteach=mysql_fetch_array($query)) { $id=$getdata[0]; $title=$getdata[1]; $content=$getdata[2]; $qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content); }
i believe should be:
while($getdata=mysql_fetch_array($query)) { $id=$getdata[0]; $title=$getdata[1]; $content=$getdata[2]; $qa[]=array('id'=> $id, 'title'=> $title, 'content'=> $content); }
because otherwise $getdata
empty.
Comments
Post a Comment