【问题标题】:live search in php with an error in mysqli_stmt_close在 php 中进行实时搜索,mysqli_stmt_close 出现错误
【发布时间】:2018-11-03 07:16:29
【问题描述】:

大家好,我只想问。我想进行实时搜索,但此代码不起作用。当我输入一些东西并且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);
?>

谁能帮帮我。谢谢。。

【问题讨论】:

  • 首先,在输入每个键后启动请求的方法不正确,您需要做的是在页面加载时加载所有基本数据并使用 select2 对其进行过滤,否则您可以启动当用户完成输入查询并按下搜索按钮时的搜索请求
  • 我的代码有什么问题先生。我刚从我搜索的内容中得到它,然后我编辑。
  • 好的,所以你的代码正在做的是,每次当用户按下某个键时,它都会向你的服务器发起一个获取请求,并且可以确保旧的请求没有完成并且用户会按下另一个键,所以这不是正确的方法。
  • 我现在该怎么办?
  • 因为根据教程这会起作用

标签: php jquery html mysql


【解决方案1】:

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

<?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);
?>

【讨论】:

  • 我应该在变量 $val 中输入什么值,因为你等于 "Mr.ram"
猜你喜欢
  • 2015-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
  • 2023-03-03
相关资源
最近更新 更多