drfqfuhej48511519 2015-05-03 18:03
浏览 26
已采纳

无法通过AJAX请求运行SQL查询,使用相同类型的AJAX查询,除此之外的其他地方都可以正常工作

I'm using this AJAX call everywhere else and it works like a charm but only this gives problem. I don't understand what is the problem? Why I'm getting "Error retried record:" still I don't see any problem and so easy.

PS: I echoed the values at AJAX side response and getting all three values there but database gives problem. More, same SQL query works fine at database side but when it deals with AJAX and PHP, it gives problem.

Values passed fine:

id1=get-version&id2=machine055&id3=vmlab

Database query seems fine:

SELECT `version` FROM `table1` where `machine` = 'machine055'

PHP+UI:

serverChange.on('change', function(){
            $("#progress").show();
            var selectedServer = $(this).val();

            $.ajax({
                type: "POST",
                url: "/web/scripts.php",
                data: { id1: "get-version", id2: selectedServer, id3: "vmlab" },
                success:function(msg){
                    $("#progress").hide();
                    alert(msg);
                }
            });

        });

PHP+Ajax:

if ($_POST['id1'] == "get-version" && $_POST['id3'] == "vmlab") {

    $conn = mysqli_connect("localhost", "root", "pwd", "db");
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT `version` FROM `table1` where `machine` = '" .trim($_POST['id2']). "'";
    if ($conn->query($sql) === TRUE) {
        echo "Machine retrieved successfully.";
    } else {
        echo "Error retried record: " . $conn->error;
    }
  • 写回答

1条回答 默认 最新

  • doushan7077 2015-05-03 20:48
    关注

    I tried different approach and its working fine so not exploring much, here is the solution:

        $conn = mysqli_connect("localhost", "root", "pwd", "db");
         if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
    
        $sql = "SELECT `version` FROM `table1` where `machine` = '" .trim($_POST['id2']). "'";
        $result = mysqli_query($conn, $sql);
    
        while($row =  mysqli_fetch_array($result)) {
            $rows[] = $row['version'];
        }
    
        echo ($rows[0]);
    
        $conn->close();
    

    Thanks all for reading the issue and resolution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题