0

我正在尝试对列执行 FULLTEXT 搜索,如果第一个返回零行,则在布尔模式下进一步 FULLTEXT 搜索。如果我在命令行中手动将 SQL 查询输入 MySQL,我会得到我期望的结果,但是这段代码将一个空数组返回给我的 JS。有什么想法我要去哪里吗?

$con = mysqli_connect( $db_url, $db_user, $db_pwd, $db );
if( !$con ) {
error_log("Connection failed!");
die();
}

$sql = "SELECT problem, note_text, doctor, entry_date FROM notes WHERE mrn=? AND MATCH(note_text) AGAINST(?)";

$stmt = mysqli_prepare( $con, $sql );
mysqli_stmt_bind_param( $stmt, 'is', $_POST['mrn'], $_POST['search_text'] );
$success = mysqli_stmt_execute( $stmt );

if( mysqli_stmt_num_rows($stmt) == 0 ) { // Then try fulltext search in boolean mode

    $sql = "SELECT problem, note_text, doctor, entry_date FROM notes WHERE mrn=? AND MATCH(note_text) AGAINST(? IN BOOLEAN MODE)";

    $stmt = mysqli_prepare( $con, $sql );
    mysqli_stmt_bind_param( $stmt, 'is', $_POST['mrn'], $_POST['search_text'] );
    $success = mysqli_stmt_execute( $stmt );
} 

    mysqli_bind_result( $stmt, $problem, $note_text, $doctor, $date );
    $notes = array();

    for( $i = 0; mysqli_stmt_fetch($stmt) == TRUE; ++$i ) {
        $notes[$i] = array( 'problem' => $problem, 'note' => $note_text, 'doctor' => $doctor, 'date' => $date );
    }

    $json = json_encode($notes);
    echo $json; 
4

1 回答 1

0

好的,所以我想我已经找到了答案。我需要在使用第二个 STATEMENT $stmt 之前关闭第一个 STATEMENT $stmt

mysqli_stmt_close($stmt) 

刚过

if( mysqli_stmt_num_rows($stmt) == 0 )

简单地做

$stmt = mysqli_prepare( $con, $sql );

不会给你一个新的语句并覆盖以前的变量!

于 2013-07-29T18:49:55.163 回答