PHP Ajax实时搜索仅适用于localhost

时间:2018-01-25 14:13:49

标签: php ajax search mysqli

我在导航栏上创建搜索表单以搜索mysql表上的用户。

我使用了我在网上找到的脚本,当我在localhost上使用它时,一切都很顺利。一旦我输入了名字的第一个字母,我就会立即得到结果。

但现在,我将网站放在网络服务器上,会发生以下情况:

  • 我写了1到3个字母,没有任何反应;
  • 我写了4/5字母并出现错误(致命错误 :调用未定义的函数mysqli_stmt_get_result() /home/pg22933/public_html/jade/backend-search.php 在线 68);
  • 我几乎写了一个用户的全名,结果终于出现了。

这是我拥有所有代码的文件:

<?php include('headers.php'); ?>

<script type="text/javascript">

function updateentrada(value, id)
{
 console.log(value, id);
 if(value == 1)
 {
  $.ajax({
  type: 'post',
  url: 'loaddata.php',
  data: {
   idconvidado:id,
   entrada: 0
  },
  success: function () {
    location.reload(); 
  }
  });
 }

 else if (value == 0)
 {
    $.ajax({
  type: 'post',
  url: 'loaddata.php',
  data: {
    idconvidado:id,
   entrada: 1
  },
  success: function () {
    location.reload();
  }
  });
 }
}

</script>


<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "jade");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

if(isset($_REQUEST['term'])){
    // Prepare a select statement
    $sql = "SELECT * FROM convidados WHERE nome_convidado 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)){
                    $idconvidado = $row["id_convidado"];
                    $nome = $row["nome_convidado"];
                    $entrada = $row["entrada_convidado"];
                    if ($entrada == 1){
                        echo "<button value='". $entrada ."' name='". $idconvidado . "' onclick='updateentrada(this.value, this.name)' class='btnsearch'>" . $nome . " <i class='fa fa-check-circle-o check aria-hidden='true'></i></button>";
                    }
                    else if ($entrada == 0){
                        echo "<button value='". $entrada ."' name='". $idconvidado . "' onclick='updateentrada(this.value, this.name)' class='btnsearch '>" . $nome . " <i class='fa fa-circle-o check aria-hidden='true'></i></button>";
                    }
                }
            } else{
                echo "<p>No matches found</p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
        }
    }

    // Close statement
    mysqli_stmt_close($stmt);
}

// close connection
mysqli_close($link);
?>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

http://php.net/manual/en/mysqli-stmt.get-result.php

It requires the mysqlnd driver, you need to install it on your web server