脚本宝典收集整理的这篇文章主要介绍了使用PHP / MySQL创建JSON数据的正确方法,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
根据以下答案,我现在有以下PHP脚本:
header('Content-tyPE:application/json'); function getdata($the_query) { $connection = MysqL_connect('server','user','pass') or die (MysqL_error()); $db = MysqL_select_db('db_name',$connection) or die (MysqL_error()); $results = MysqL_query($the_query) or die(MysqL_error()); header('Content-type:application/json'); $the_data['RSS']['channels']['tITle'] = $title; $the_data['RSS']['channels']['link'] = $link; $the_data['RSS']['channels']['description'] = $description; while($row = MysqL_fetch_array($result)) { extract($row); $the_data['RSS']['channels']['items']['title'] = $item_title; $the_data['RSS']['channels']['items']['link'] = "$item_link; $the_data['RSS']['channels']['items']['date'] = $item_date; $the_data['RSS']['channels']['items']['description'] = $item_description; } MysqL_close($connection); return json_encode($the_data); }
返回以下内容:
{ "RSS": { "channels": { "title":"title goes here","link":"link goes here","description":"description goes here","items": { "title":"'title goes here","date":"date goes here","description":"description goes here" } } } }
<?PHP $channel = array( 'title' => 'title goes here','link' => 'link here','description' => 'description','items' => array() ); while($row = MysqL_fetch_array($results)) { extract($row); $channel['items'][] = array( 'title' => $title,'link' => $link,'guid' => $guid,'pubDate' => $date,'description' => $description ); } $channels = array($channel); $RSS = (object) array('RSS'=> array('channels'=>$channels)); $JSON = json_encode($RSS); echo $json; ?>
以上是脚本宝典为你收集整理的使用PHP / MySQL创建JSON数据的正确方法全部内容,希望文章能够帮你解决使用PHP / MySQL创建JSON数据的正确方法所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。