Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery和php从mysql数据库获取信息_Php_Jquery_Mysql_Ajax - Fatal编程技术网

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

使用jquery和php从mysql数据库获取信息,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我试图在php中使用ajax和restapi从mysql数据库获取数据。我想在搜索栏上插入一个值,它会显示来自mysql的数据 api代码: $response = array(); $posts = array(); $ID = $_POST['ID']; $sql = "SELECT * FROM table WHERE client_id = ?"; $stmt = $mysqli->prepare($s

我试图在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;

在error方法中添加一个console.errorerror,看看哪里出了问题。它什么也不显示,只是打印了这个console.logto_搜索的结果;谢谢,我做了您和@Anatolii Bivol告诉我的更改,至少现在显示了错误,Sugestions?@ShwSvn您确定要传递表中存在的ID以使其返回吗?当它与表中的任何内容都不匹配时,你就会得到这个结果。好吧,这是我的错,我现在得到了这个问题中包含数据的数组,我如何显示我在ajax上收到的数据html@IncredibleHat这是我的问题是的,谢谢