-1

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

在 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 行中的绑定变量数不匹配

需要做什么来解决这个警告?

4

2 回答 2

1

您收到此错误的原因是因为您只传入 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 的引用传递问题中得到了解决方案

于 2012-06-28T02:59:57.103 回答
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>
于 2012-06-28T04:44:37.933 回答