[英]JSON Ajax PHP MYSQL - multiple records into JSON
我給這個php腳本打電話(只顯示有問題的部分)
if (mysql_num_rows($pquery) == 1) {
$result = mysql_fetch_assoc($pquery);
$query_ht = mysql_query("SELECT * FROM coupon WHERE pid='$result[id]'");
$ht_result = mysql_fetch_array($query_ht); // THIS WILL ALWAYS RETURN 3 ROWS
//echo json_encode($result); //THis will always contain one row
//echo json_encode($ht_result); // THIS WILL ALWAYS contain 3 ROWS
更新:
//echo json_encode(array(
//'comp' => $result,
//'ht' => $ht_result)
// );
$query_htl = mysql_query("SELECT * FROM coupon WHERE pid='$result[id]' AND type='L'");
$query_htg = mysql_query("SELECT * FROM coupon WHERE pid='$result[id]' AND type='G'");
$query_htr = mysql_query("SELECT * FROM coupon WHERE pid='$result[id]' AND type='R'");
$ht_resultl = mysql_fetch_assoc($query_htl);
$ht_resultg = mysql_fetch_assoc($query_htg);
$ht_resultr = mysql_fetch_assoc($query_htr);
echo json_encode(array(
'comp' => $result,
'htL' => $ht_resultl,
'htG' => $ht_resultg,
'htR' => $ht_resultr)
);
}
當我回顯一個或另一個時,我還好。。但是我不知道如何將所有內容返回給jquery
jQuery腳本:
$('#c_search').submit(function(){
data = ($(this).serialize());
$.ajax({
url: 'actions/get_company.php',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
success: function(selected){
我可以打電話給:
alert(selected.htL.id);
只需將所有結果合並到公共數組中即可:
header('Content-type: application/json');
die(json_encode(array(
'result' => $result,
'ht_result' => $ht_result
)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.