php - 在 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);
?>
有人能帮助我吗。谢谢你..
解决方案
你的方法不对,试试这个方法。。
<?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);
?>
推荐阅读
- reactjs - Redux 表单:使用组件道具中的参数验证 NumberInput
- javascript - 使用 jQuery/Javascript 在 HTML 表中插入 Json 响应
- android - 在应用程序中显示 FTP 图像而无需下载它们
- javascript - 页面上每个轮播的独特流畅轮播设置
- reactjs - 使用请求查询参数,反应路由器
- java - Swagger Ui 强制输入作为文件上传类型而不是文本
- java - 如果我想禁用测试,如果测试方法下方存在一些注释,如何处理 ExecutionCondition?
- labview - 如何将 5 个元素的簇保存到 XML 文件中的一个属性元素中?
- javascript - 检查部分圆弧是否与圆相交
- pdfbox - 如何使用 pdfbox 或任何其他库将 pdf 转换为 xml?