dsiuz86842 2014-11-04 16:52
浏览 72

使用ajax php mysql从各种查询中检索多个数据

I'm new in Ajax and JSON notation, so I'm trying to get data from differents tables of a Database, data like country names, state names, departament name, job position etc. and I've seen examples how through JSON can get data but just from a single table, can you give me a little help how can I do it with more than one table and keep it in an array.


  $host = "localhost";
  $user = "usuer";
  $pass = "password";

  $databaseName = "jsonExample";
  $tableName = "variables";

  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);

  $result = mysql_query("SELECT * FROM $tableName");            //query
  //$array = mysql_fetch_row($result);                          //fetch result
    if(mysql_num_rows($result) <= 0){

        while($obj = mysql_fetch_row($result)){
        $array[] = $obj;        

  echo json_encode($array);


Html file:

    <script language="javascript" type="text/javascript" src="jquery.js"></script>

  <h2> Client example </h2>
  <h3>Output: </h3>
  <div id="output">this element will be accessed by jquery and this text will be replaced</div>

  <script id="source" language="javascript" type="text/javascript">

  $(function () 

      url: 'api.php',                  //the script to call to get data          
      data: "",                        //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply

        var id = data[0];              //get id
        var vname = data[1];           //get name

        $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname);     //Set output element html
        //recommend reading up on jquery selectors they are awesome http://api.jquery.com/category/selectors/


  • 写回答

1条回答 默认 最新


      相关推荐 更多相似问题


      • ¥15 关于#tensorflow#的问题,如何解决?
      • ¥15 VBA中使用FindWindowEx函数如何向网页表单中的多个文本框中的某一个文本框发送消息
      • ¥15 关于#glmb#的问题,如何解决?
      • ¥15 TI的TMS320F28335,RS485串口进行SCI-B通信出错
      • ¥15 XML文件报错不允许出现此特性,百度也搜不到解决方法
      • ¥15 FTPC共同交流学习
      • ¥15 关于#python#的问题:或者是我换其他什么模块可以解决这个问题图如下:
      • ¥15 用html加css制作一个网页
      • ¥200 MFC中如何对ListCtrl的某一列的文本能换行显示
      • ¥15 clion添加库文件