2014-11-04 73 views
0

我是新的Ajax和JSON表示法,所以我試圖從數據庫的不同表中獲取數據,數據如國家名稱,州名,出事名稱,工作位置等等,我見過的例子是如何通過JSON獲取數據,但只是從一個表中,你能給我一點幫助,我怎樣才能用多個表來完成它,並將它保存在一個數組中。從各種查詢檢索多個數據與AJAX php mysql

<?php 


    $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){ 

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

    echo json_encode($array); 

?> 

HTML文件:

<html> 
    <head> 
    <script language="javascript" type="text/javascript" src="jquery.js"></script> 
    </head> 
    <body>--> 


    <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() 
    { 


    $.ajax({          
     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/ 
     } 
    }); 

    }); 
    </script> 

    </body> 
</html> 
+0

mysql_fetch_row在這種情況下無用。改爲使用mysql_fetch_assoc。您可以獲得關聯的json屬性。 data.name,data.country,data.id ... – Panoptik 2014-11-04 16:59:41

回答

0

如果你想擁有從一個陣列可以每個結果添加到一個關鍵的多個查詢結果。 F.i.如果您查詢表table1到tablen ...

// define the array that will contain all result sets 
$array = []; 

// create an array for the result set coming from table 1 
$array['table1']= []; 
$result = mysql_query("SELECT * FROM table1"); 
    if(mysql_num_rows($result) <= 0){ 
}else{ 
    while($obj = mysql_fetch_row($result)){ 
    $array['table1'][] = $obj;   
    } 
} 

// create an array for the result set coming from table 2 
$array['table2']= []; 
$result = mysql_query("SELECT * FROM table2"); 
    if(mysql_num_rows($result) <= 0){ 
}else{ 
    while($obj = mysql_fetch_row($result)){ 
    $array['table2'][] = $obj;   
    } 
} 
:: 
:: 
// create an array for the result set coming from table n 
$array['tablen']= []; 
$result = mysql_query("SELECT * FROM tablen"); 
    if(mysql_num_rows($result) <= 0){ 
}else{ 
    while($obj = mysql_fetch_row($result)){ 
    $array['tablen'][] = $obj;   
    } 
} 

// return the results formatted as json 
return json_encode($array); 

在javascript中您可以使用data->table1訪問結果表1。

提示

使用mysqli代替mysql。它是mysql的改進版本。在某些背景下檢查this question的答案。