我需要PHP代碼將數據保存到JSON文件。如何使用MySQL數據庫在PHP中創建一個JSON arrray數據庫
所需結果: 「日曆」:[{ 「Pariharam」: 「pariharam」, 「Palamozhi」: 「palamozhi」, 「八字」:[ { 「HoroscopeID」: 「daily_horoscope_id」, 「Rasiname」: 「拉西」, 「SHORTDESCRIPTION」: 「shot_desc」, 「LongDescription」: 「long_desc」 }, } ]
我的代碼:
$sql=mysql_query("select * from daily_calendar_tbl");
$response = array();
$posts = array();
//$result=mysql_query($sql);
while($row=mysql_fetch_array($sql))
{
$caldate=$row['cal_date'];
$tamilyear=$row['tamil_year'];
$tamilmonth=$row['tamil_month'];
$tamildate=$row['tamil_date'];
$gtms=$row['gt_mrg_str'];
$gtme=$row['gt_mrg_end'];
$gtes=$row['gt_eve_str'];
$gtee=$row['gt_eve_end'];
$ggtms=$row['gow_mrg_str'];
$ggtme=$row['gow_mrg_end'];
$ggtes=$row['gow_eve_str'];
$ggtee=$row['gow_eve_end'];
$rst=$row['ragu_str'];
$ret=$row['ragu_end'];
$gst=$row['guli_str'];
$get=$row['guli_end'];
$yst=$row['yema_str'];
$yet=$row['yema_end'];
$sut=$row['sur_udha'];
$suras=$row['sur_astha'];
$yogam=$row['yogam'];
$chantrs=$row['chandrashtama'];
$soolam=$row['soolam'];
$phariharam=$row['pariharam'];
$palamozhi=$row['palamozhi'];
$selrasi=mysql_query("select * from daily_horoscope_tbl where horoscope_date='$caldate'");
while($rasidata=mysql_fetch_array($selrasi)){
$horoid=$rasidata["daily_horoscope_id"];
$rasiname=$rasidata["rasi"];
$shortdesc=$rasidata["shot_desc "];
$long_desc=$rasidata["long_desc"];
$horoscopedata[]=array('HoroscopeID'=> $horoid, 'Rasiname'=> $rasiname, 'ShortDescription'=>$shortdesc,'LongDescription'=> $long_desc);
}
$horoscopesres=$horoscopedata;
$posts[] = array('CalendarDate'=> $caldate, 'TamilYear'=> $tamilyear, 'TamilMonth'=>$tamilmonth,'TamilDate'=> $tamildate, 'GoodTimeMorningStart'=> $gtms,
'GoodTimeMorningEnd'=>$gtme,'GoodTimeEveningStart'=> $gtes, 'GoodTimeEveningEnd'=> $gtee, 'GowriGoodTimeMorningStart'=>$ggtms,'GowriGoodTimeMorningEnd'=> $ggtme, 'GowriGoodTimeEveningStart'=> $ggtes,
'GowriGoodTimeEveningEnd'=>$ggtee,'RahuStartTime'=> $rst, 'RahuEndTime'=> $ret, 'GulikaiStartTime'=>$gst,'GulikaiEndTime'=> $get, 'YamagandamStartTime'=> $yst,
'YamagandamEndTime'=>$yet,'SuryaUdhayamTime'=> $sut, 'SuryaAsthamanam'=> $suras, 'Yogam'=>$yogam,'Chandrashtama'=> $chantrs, 'Soolam'=> $soolam, 'Pariharam'=>$phariharam,'Palamozhi'=>$palamozhi,'horoscopes'=>$horoscopesres);
}
$response['calendardata'] = $posts;
$fp = fopen('articles.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
你可以通過數組變量'json_encode()的數組轉換成JSON'函數 –
請提供你的代碼已經嘗試了,使我們可以幫助您更好。 – gmuraleekrishna