AJAX 实时搜索正在重复结果

AJAX live search is duplicating results

我有这个脚本,它从输入字段读取文本,然后将值发送到 php 文件以搜索数据库(“pharmacie”)中的所有表。我在不同的表中有相同的数据(例如:“ABILIFY 10MG COMP.B/28”存在于两个表中)。我如何只显示一次。 我试过 SELECT DISTINCT。

Results output

<script>
    $(document).ready(function() {
      $('.search input[type="text"]').on("keyup input", function() {
        /* Get input value on change */
        var inputVal = $(this).val();
        var resultDropdown = $(this).siblings(".result");
        if (inputVal.length) {
          $.get("livesearch.php", {
            term: inputVal
          }).done(function(data) {
            // Display the returned data in browser
            resultDropdown.html(data);
          });
        } else {
          resultDropdown.empty();
        }
      });

      // Set search input value on click of result item
      $(document).on("click", ".result p", function() {
        $(this).parents(".search").find('input[type="text"]').val($(this).text());
        $(this).parent(".result").empty();
      });
    });
  </script>

livesearch.php

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "pharmacie");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$tables = mysqli_query($link, "SHOW TABLES");
while ($table = mysqli_fetch_object($tables))
{
    $table_name = $table->{"Tables_in_pharmacie"};
 
if(isset($_REQUEST["term"])){
    // Prepare a select statement
    $sql = "SELECT * FROM " . $table_name . " WHERE Nom_medicine LIKE ?";
    
    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_term);
        
        // Set parameters
        $param_term = $_REQUEST["term"] . '%';
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);
            
            // Check number of rows in the result set
            if(mysqli_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    echo "<p>" . $row["Nom_medicine"] . "</p>";
                }
            } 
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
}
 
}
// close connection
mysqli_close($link);
?>

您遍历所有 table,并回显每个 table 的结果,这就是 select distinct 不起作用的原因。您可以回信,结果您已经返回并跳过它们。

我可能不会那样解决它,您可以重写脚本以将所有这些存档在一个 select 中,或者您甚至可以直接在 [=17] 中排除已经打印的内容=] 查询以获得更快的性能,但作为快速修复,这应该适合您:

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "pharmacie");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
$alreadyReturned = [];
$tables = mysqli_query($link, "SHOW TABLES");
while ($table = mysqli_fetch_object($tables))
{
    $table_name = $table->{"Tables_in_pharmacie"};
 
if(isset($_REQUEST["term"])){
    // Prepare a select statement
    $sql = "SELECT * FROM " . $table_name . " WHERE Nom_medicine LIKE ?";
    
    if($stmt = mysqli_prepare($link, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt, "s", $param_term);
        
        // Set parameters
        $param_term = $_REQUEST["term"] . '%';
        
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            $result = mysqli_stmt_get_result($stmt);
            
            // Check number of rows in the result set
            if(mysqli_num_rows($result) > 0){
                // Fetch result rows as an associative array
                while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
                    if (in_array($row["Nom_medicine"], $alreadyReturned, true))
                    {
                        continue;
                    }
                    echo "<p>" . $row["Nom_medicine"] . "</p>";
                    $alreadyReturned[] = $row["Nom_medicine"];
                }
            } 
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
}
 
}
// close connection
mysqli_close($link);
?>