1.php程序 <?php mysql_connect("localhost","root",""); mysql_select_db("myhospitalv2"); $q=mysql_query("SELECT * FROM user2 "); while($e=mysql_fetch_object($q)) $output[]=$e; print(json_encode($output)); mysql_close(); ?> 2.瀏覽器返回結果 [{"id":"1","name":"Tony","sex":"1","birthyear":"1999"},{"id":"2","name":"Jim","sex":"1","birthyear":"1987"},{"id":"3","name":"Lucy","sex":"0","birthyear":"1988"},{"id":"4","name":"Tom","sex":"1","birthyear":"1980"}] 3.總結 使用php引擎操做MySQL數據庫,若返回結果爲記錄集,將記錄集中記錄進行從新存儲,如上 $q=mysql_query("SELECT * FROM user2 "); while($e=mysql_fetch_object($q)) $output[]=$e; 而後調用json_encode()進行JSON數據封裝,可獲得正確的JSON數據格式,用於不一樣設備間的數據傳輸。 轉載請註明:http://www.spasvo.com/news/html/20131217104641.html