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数据格式,用于不同设备间的数据传输。