我用 PHP 编写了一个简单的搜索脚本。运行时出现空白屏幕并且 PHP 错误日志状态
绑定变量的数量与准备好的语句中的字段数量不匹配
我不明白为什么会这样说,因为我绑定了一个字段 - $searchTitle 和一个绑定参数(字符串)。我的标记很可能完全错误。
任何建议表示赞赏。谢谢。
<?php
if (isset($_POST['btn-search-article'])) {
$searchTitle = $_POST['search-title'];
include_once("mysqli_connect.php");
$table = "articles";
$sql = "SELECT title,
MATCH(title,category,artbody)
AGAINST ('?')
AS score
FROM $table
ORDER BY score DESC";
$stmt = mysqli_stmt_init($dbh);
if (!(mysqli_stmt_prepare($stmt, $sql))) {
echo("Write to DB failed ");
}
else { //statement prepared ok
mysqli_stmt_bind_param($stmt, 's', $searchTitle);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $s1);
while (mysqli_stmt_fetch($stmt)) {
echo($s1 . "<br>");
}
mysqli_stmt_close($stmt);
}
} else {
echo("Error: Search could not be completed");
}
?>
注意:PHP 错误日志指出错误在线
mysqli_stmt_bind_result($stmt, $s1);
修复:似乎 sql 语句的顺序导致了错误。应该
$sql = "SELECT title
FROM $table
WHERE MATCH (title, category, artbody)
AGAINST (?)";