Ajax 搜索没有连接到数据库

Ajax search doesnt connect to database

我正在尝试进行实时 ajax 搜索。当您输入一个词时,它会自动在下面显示建议,就像 w3schools 一样。但出于某种原因,我的索引文件和我的 php 没有交换数据值,或者我的数据库没有连接某些 reason.What 我总是得到的是 "no country found"。你能检查代码是否有错误吗? 这是 php 文件:

<?php
include_once('dbconnect.php');
$q = intval($_GET['q']);
    $query = mysqli_query($conn,"SELECT * FROM `users` WHERE userCountry LIKE '%".$q."%'"); 
        $count = mysqli_num_rows($query);
//Replace table_name with your table name and `thing_to_search` with the column you want to search
    if($count == "0" || $q == ""){
        $s[] = "No Country found!";
    }else{
                while($row = mysqli_fetch_array($query)){
        $s[] = $row['userCountry']; // Replace column_to_display with the column you want the results from
                 } 
        }
for($x = 0; $x < $count; $x++) {
          echo $s[$x];
          echo "<br>";
}
?>

这是我的 index.php 文件:

<head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
function showCountry(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","indexsearchquery.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
<input id="search-box" name="q" type="text" autocomplete="off" placeholder="Search country..." onchange="showCountry(this.value)" />
       <input type='image' name='search' id="search-icon" value='Submit' src="search-icon.png" >
       <p style="color:white;">Suggestions: <span id="txtHint" ></span></p>

您的国家名称不是整数。但是你把它转换成 intval 把你的 php 文件改成

include_once('dbconnect.php');
$q = $_GET['q']; //<-----remove intval
    $query = mysqli_query($conn,"SELECT * FROM `users` WHERE userCountry LIKE '%".$q."%'"); 
        $count = mysqli_num_rows($query);
//Replace table_name with your table name and `thing_to_search` with the column you want to search
    if($count == "0" || $q == ""){
        $s[] = "No Country found!";
    }else{
                while($row = mysqli_fetch_array($query)){
        $s[] = $row['userCountry']; // Replace column_to_display with the column you want the results from
                 } 
        }
for($x = 0; $x < $count; $x++) {
          echo $s[$x];
          echo "<br>";
}

了解 dbconnect.php 文件的内容很重要,因为它包含数据库连接变量。 检查您的 MySQL 服务器是否启动以及所有变量是否设置正确。