好的,这很奇怪,但我会解释发生了什么。
在 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 行中的绑定变量数不匹配
需要做什么来解决这个警告?