douqianni4080 2018-01-31 16:16
浏览 60

使用jquery和php从mysql数据库获取信息

I'm trying to get data from mysql database using ajax and restapi in php. I wanted that I inserted a value on a search bar and it shows me the data from mysql

api code:

        $response = array();
        $posts = array();
        $ID = $_POST['ID'];
        $sql = "SELECT * FROM table WHERE client_id = ?";
        $stmt = $mysqli->prepare($sql);
        $stmt->bind_param("s", $ID);
        $stmt->execute();
        $result= $stmt->get_result();
        $row = $result->fetch_assoc();
        while($row=mysql_fetch_array($result)) { 
          $user_id=$row['id'];
          $username=$row['username'];   
          $client_id=$row['client_id']; 
          $token=$row['token'];  

          $posts = array('id'=> $user_id, 'username'=> $username, 'client_id'=> $client_id, 'token'=> $token);
        } 

        echo json_encode($posts);

        $stmt = null;   
        $mysqli = null;
    }

?>

And this is the ajax code

$('document').ready(function()
                { 
                    $("#search").on("click", function(e){
                        e.preventDefault();
                        var to_search = $("#search_value").val();
                        console.log(to_search);

                        var formData = {
                        'ID' : to_search,
                      };

                      $.ajax({
                        type : 'POST',
                        url : 'search.php',
                        data : formData,
                        dataType : 'JSON',
                        encode : true,
                        success: function (data, response, status, xhr) {
                          if (response.result) {
                            console.log('done');
                            console.log(data);
                          }else{
                            console.log('fail');
                            console.log(data);
                          }
                        },
                        error: function (xhr, status, error) {

                        }
                                });              
                             });    
                  });

It's not working, I would just like that it print the result on the browser console. The button is working because it shows on the console the value that I inserted on the search bar Any advices?

  • 写回答

1条回答 默认 最新

  • dqphg40600 2018-01-31 16:27
    关注

    I don't see any problem in your ajax call, however i did some amendments to your PHP code (this is untested).

        $response = array();
        $posts = array();
        $ID = isset($_POST['ID']) ? $_POST['ID'] : 0; // Avoid warning because of missing "ID" post field
        $sql = "SELECT * FROM table WHERE client_id = ?";
        $stmt = $mysqli->prepare($sql);
        $stmt->bind_param('i', $ID); // ID's probably an integer, not a string
        $stmt->execute();
        $result= $stmt->get_result();
        // Removed $row = $result->fetch_assoc();
        while($row = $result->fetch_assoc()) { // Better use it this way
            $user_id = $row['id'];
            $username = $row['username'];   
            $client_id = $row['client_id']; 
            $token = $row['token'];  
    
            array_push($posts, array('id'=> $user_id, 'username'=> $username, 'client_id'=> $client_id, 'token'=> $token)); // Add the current loop element data to $posts
        } 
    
        echo json_encode($posts);
    
        $stmt = null;   
        $mysqli = null;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用