使用mysqli准备的语句错误和不匹配的绑定变量进行全文搜索

时间:2012-07-27 12:47:34

标签: php full-text-search mysqli prepared-statement

我在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);

FIXED:似乎sql语句的顺序导致了错误。应该是

$sql = "SELECT title
        FROM $table
        WHERE MATCH (title, category, artbody)
        AGAINST (?)";

0 个答案:

没有答案