繁体   English   中英

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

[英]Get info from mysql database with jquery and php

我正在尝试使用php中的ajax和restapi从mysql数据库中获取数据。 我想在搜索栏上插入一个值,然后向我显示来自mysql的数据

api代码:

        $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;
    }

?>

这是ajax代码

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

                        }
                                });              
                             });    
                  });

它不起作用,我只想在浏览器控制台上打印结果。 该按钮起作用是因为它在控制台上显示了我在搜索栏中插入的值。有什么建议吗?

我在您的ajax调用中没有看到任何问题,但是我对您的PHP代码做了一些修改(未经测试)。

    $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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM