douhan8009 2015-03-11 10:56
浏览 52
已采纳

使用jquery ajax检索并显示多个不同的mysql查询

I have several div tags that I would like to be updated with several different mysql queries.

My code below only updates one of these divs, how can I expand it to 1-6 and also leave room for expansion in the future for more divs?

I also need to be able to update each div individually with a specific query for a specific table.

<html>

<div id="1"> </div>
<div id="2"> </div>
<div id="3"> </div>
<div id="4"> </div>
<div id="5"> </div>
<div id="6"> </div>
//...and so on

</html>

<script type="text/javascript">

$(document).ready(function () {
function display($id) {
    $.ajax({
        async: false,
        type: "POST",
        url: "apifile.php",
        data: {id:$id},
        dataType: "json",
        success: function(msg) {
            if(msg.success) {
                $($id).next(".one").html(msg);
            } else {
                alert("error");
            }
        }
    });
}

</script>

<?php

mysql_connect(host, user, pass);
mysql_select_db(name);
mysql_query("select round((count(*)*100)/(select count(*) from test),1) as percent from test group by field1 order by percent desc");

$reply['success'] = "Success"; 
if($return = display($id)) {
    $reply['success'] = "Success";
} else {
    $reply['error'] = "Error";
}
echo json_encode($reply);

?>
  • 写回答

2条回答 默认 最新

  • douli2063 2015-03-11 11:15
    关注

    you can store the data/messages/otherinfo from each query in different portions of your $reply array

    query1 goes here ...
    
       $reply[1]['success'] = "Success"; 
    if($return = display($id)) {
        $reply[1]['success'] = "Success";
    } else {
        $reply[1]['error'] = "Error";
    }
    
    query2 goes here ...
    
    $reply[2]['success'] = "Success"; 
    $reply[2]['data'] = "3,4,5"; 
    
    query3 goes here ...
    
    $reply[3]['success'] = "Success"; 
    $reply[3]['data'] = "beans,soup,guac"; 
    
    echo json_encode($reply);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题