首页 > 解决方案 > 在 php 中进行实时搜索,mysqli_stmt_close 出现错误

问题描述

大家好,我只想问。我想进行实时搜索,但此代码不起作用。当我输入一些东西并且mysqli_stmt_close中也有错误时没有任何反应。有人能帮我吗?这是我的代码。提前致谢。

这是我的页面。

<script type="text/javascript">
$(document).ready(function(){
$('.search-box 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("backend-search.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-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>

<div class="search-box">
<input type="text" autocomplete="off" placeholder="Search Book..." />
<div class="result"></div>
</div>

这是我的实时搜索过程的代码。

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

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

if(isset($_REQUEST["term"])){
// Prepare a select statement
$sql = "SELECT * FROM tbl_books WHERE BookTitle 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["BookTitle"] . "</p>";
}
} 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);
?>

有人能帮助我吗。谢谢你..

标签: phpjqueryhtmlmysql

解决方案


你的方法不对,试试这个方法。。

<?php
   $link = mysqli_connect("localhost", "root", "", "Database");

   /* check connection */
   if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
   }

   $val= "Mr.ram";

   /* create a prepared statement */
   if ($stmt = mysqli_prepare($link, "SELECT value FROM data WHERE Name=?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $val);

    /* execute query */
    mysqli_stmt_execute($stmt);

   /* bind result variables */
   mysqli_stmt_bind_result($stmt, $district);

   /* fetch value */
   mysqli_stmt_fetch($stmt);

   printf("%s is in district %s\n", $val, $district);

   /* close statement */
   mysqli_stmt_close($stmt);
   }

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

推荐阅读