Php 为什么在尝试搜索术语时没有出现记录?
好吧,这很奇怪,但我会解释发生了什么 在SQL中,假设我想使用LIKE语句在数据库中查找2个术语(AAA、AAB),能够查找这些术语结果的查询如下:Php 为什么在尝试搜索术语时没有出现记录?,php,mysqli,Php,Mysqli,好吧,这很奇怪,但我会解释发生了什么 在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.QuestionConte
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;
}
}
?>
参数输出:
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都需要有一个单独的param。
很明显,这有点困难,因为它可能是一个可变的量。
因此,替换
$stmt->bind_param($paramString, );
与
我还没有测试过,所以不确定它是否有效。
我从你那里得到了答案
再看看你的代码,你有一些逻辑错误 (一) 应该是
$searchquestion = $_GET['questioncontent'];
if (empty($searchquestion)){
(二)
应该是
$searchquestion = $_GET['questioncontent'];
if (empty($searchquestion)){
(三)
{$dbQuestionContent['QuestionContent']}
应该是
<td class='questiontd'>$dbQuestionContent</td>
$dbQuestionContent
这是什么,你第十次发布这条代码还是第十三次?我数不清了。人们给了你很多建议,而你却继续忽视它们。如果你没有得到你想要的答案,也许你应该重新考虑你的想法design@Kris ??? 我只问过一次这个问题,第二次发布了这个代码,你确定你没有把我误认为是其他有类似代码的人吗?那么为什么这个代码看起来几乎和我一样?我猜它是在不同的用户名下,但是拜托..@Kris haha,我认为有人发布相同代码的原因是因为我们三个人在一个小组里一起工作。我想我们小组的一个成员也必须有一个SO帐户,并问了一个关于相同代码的问题,但不是我:)哦,好的。那么对不起。。我刚刚在这里至少识别了7次代码嗨,克里斯,我已经测试了你礼貌地给我的代码,当我用你给我的代码替换代码时,它给了我一个致命错误,声明:致命错误:调用未定义的函数makeValuesReferenced()。。。在第87行
是的,我复制了您答案中最后一个代码块中的代码,并将其放入我的代码中。你想让我把call_user_func_数组放入$stmt->bind_param($paramString,)
或完全替换$stmt->bind_param($paramString,)代码>调用用户函数数组?完全替换它。删除整行$stmt->bind_param我不知道当您将函数复制到代码中时,它怎么可能是一个未定义的函数。您复制了整个函数makeValuesReferenced。。。正当请用新代码更新您的问题P,我将在问题中发布更新的代码供您查看。这很奇怪
if (empty($searchquestion)){
<td class='questiontd'>{$dbQuestionContent['QuestionContent']}</td>
<td class='questiontd'>$dbQuestionContent</td>