php开发中,想自己写一个mysql+ajax的省市县三级联动,一切都写好了,就等着返回xml或者json格式的数据了。

查询是:$sql="select * from cities where sid=1";//1为江苏省的id.这个不用多说了

$res=mysql_query($sql);

下面如何处理,用while循环还是怎么弄呢?我要的xml格式类似于



转自别人的问答,。。。

  1. 在发一次包括数据库在内的代码以示感谢!!!
  2. php页面:
  3. header("Content-Type: text/html;charset=utf-8");
  4. //告诉浏览器不要缓存数据
  5. header("Cache-Control: no-cache");
  6. $conn=mysql_connect("localhost","root","123");
  7. mysql_select_db("ajax");
  8. mysql_query("set names utf8");
  9. if(isset($_POST['sid'])){
  10. $sid=$_POST['sid'];
  11. //file_put_contents("D:/a.txt",$sid);
  12. $sql_shi="select shi from shi where sid=$sid";
  13. $res_shi=mysql_query($sql_shi);
  14. $arr="";
  15. while($row=mysql_fetch_array($res_shi)){
  16. $arr[]=$row;
  17. }
  18. $a=json_encode($arr);
  19. echo '{"aa":'.$a.'}';
  20. }
  21. ?>
  22. js页面:
  23. html页面:
复制代码


09-02 12:15