为什么尝试搜索术语时没有显示任何记录


Why is no records appearing when try to search for terms?

好吧,这很奇怪,但我会解释发生了什么。

在SQL中,假设我想使用LIKE语句在数据库中查找2个术语(AAA,AAB),能够找到这些术语结果的查询如下:

SELECT q.QuestionContent
FROM Question q
WHERE q.QuestionContent LIKE '%AAA%'
OR q.QuestionContent LIKE '%AAB%'
GROUP BY q.QuestionId, q.SessionId
ORDER BY IF( q.QuestionContent LIKE '%AAA%', 1, 0 ) , IF( q.QuestionContent LIKE '%AAB%', 1, 0 )

所以我知道SQL有效。所以我想做的是将此查询包含在 MYSQLi 中。唯一的区别是用户可以在搜索框中输入他们的术语,然后提交搜索框。因此,用户可以输入 1 个术语、2 个术语 3 个术语等,它可以是任意数量的术语。

因此,以下是我创建的MYSQLi代码,以便能够执行此操作:

        <form action="previousquestions.php" method="get">
              <p>Search: <input type="text" name="questioncontent" value="<?php echo $questioncontent; ?>" onchange="return trim(this)" /></p>
              <p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>
              </form>

        <?php 
        if (isset($_GET['searchQuestion'])) {
        $searchquestion = $questioncontent;
        $terms = explode(" ", $searchquestion);
        $questionquery = "SELECT q.QuestionContent FROM Question q WHERE ";
        $i=0;
        $whereArray = array();
        $orderByArray = array();
        $orderBySQL = "";
        $paramString = "";
        //loop through each term
        foreach ($terms as &$each) {
            $i++;
            //if only 1 term entered then perform this LIKE statement
            if ($i == 1){
                $questionquery .= "q.QuestionContent LIKE ? ";
            } else {
                //If more than 1 term then add an OR statement
                $questionquery .= "OR q.QuestionContent LIKE ? ";
                $orderBySQL .= ",";
            }
            $orderBySQL .= "IF(q.QuestionContent LIKE ? ,1,0)"; 
            $whereArray[] = "%" . $each . "%";
            $orderByArray[] = "%" . $each . "%"; 
            $paramString .= "ss";
        }  
        $questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY " . $orderBySQL; 
        $stmt=$mysqli->prepare($questionquery)or die($mysqli->error); 
function makeValuesReferenced(&$arr){ 
    $refs = array(); 
    foreach($arr as $key => $value) 
        $refs[$key] = &$arr[$key]; 
    return $refs; 
}
call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced(array_merge((array)$paramString,$whereArray, $orderByArray)));
    $stmt->execute();
    $stmt->bind_result($dbQuestionContent); 
    $questionnum = $stmt->num_rows();
    echo $questionquery;
    echo $paramString;
//OUTPUT RECORDS
        if (empty($questioncontent)){
    echo "Please enter in a phrase in the text box in able to search for a question";
}
        else if($questionnum ==0){
    echo "<p>Sorry, No Questions were found from this Search</p>";
    }
    else{
            $output = "";
    $output .= "
        <table border='1' id='resulttbl'>
          <tr>
          <th class='questionth'>Question</th>
          </tr>
    ";
            while ($stmt->fetch()) {
    $output .= "
          <tr>
          <td class='questiontd'>{$dbQuestionContent['QuestionContent']}</td>
          </tr>";
            }
            $output .= "        </table>";
            echo $output;
      }
}
            ?>

问题是,如果用户输入正确的术语,它不会显示包含这些术语的数据库中的记录。它不输出任何内容。这没有意义,因为假设我在搜索框"AAA AAB"中输入了 2 个术语,当我回显查询和参数时,它似乎是正确的,因为它输出以下内容:

查询输出:

SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE ? OR q.QuestionContent LIKE ? GROUP BY q.QuestionId, q.SessionId ORDER BY IF(q.QuestionContent LIKE ? ,1,0),IF(q.QuestionContent LIKE ? ,1,0)

参数输出:

ssss

所以我的问题是,如果查询正确并且参数数量正确,发生了什么导致成功搜索没有记录出现?

目前,我收到警告,如下所示:

警告:mysqli_stmt::bind_param() [mysqli-stmt.bind-param]:类型定义字符串中的元素数与 ...在第 87 行

需要执行哪些操作才能修复此警告?

您收到此错误的原因是因为您只传入了 1 个元素.. 一个数组。

$stmt->bind_param($paramString, array_merge($whereArray, $orderByArray));

它需要看起来像这个

$stmt->bind_param($paramString, $param1, $param2, $param3 ...);

$paramString中的每个 s 都需要有一个单独的参数。现在显然这有点困难,因为它可以是可变的金额。所以更换

$stmt->bind_param($paramString, );

$ref = array_merge((array)$paramString, $whereArray, $orderByArray);
call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($ref));
function makeValuesReferenced(&$arr){ 
    $refs = array(); 
    foreach($arr as $key => $value) 
        $refs[$key] = &$arr[$key]; 
    return $refs; 
}

我还没有测试过它,所以不确定它是否有效。我从PHP 5.3.1 的引用传递问题

同时查看您的代码,您会遇到一些逻辑错误。

1)

$searchquestion = $questioncontent;

应该是

$searchquestion = $_GET['questioncontent'];

2)

if (empty($questioncontent)){

应该是

if (empty($searchquestion)){

3)

<td class='questiontd'>{$dbQuestionContent['QuestionContent']}</td>

应该是

<td class='questiontd'>$dbQuestionContent</td>