我的Mysqli代码没有显示成功搜索的结果。由于这是在MYSQL中测试的,因此查询在语句方面是正确的。但是当试图使用mysqli时,我似乎无法在搜索成功后获得结果。我究竟做错了什么?我在进行错误报告时没有错误,并且查询中没有拼写错误。我不知道是不是因为查询需要在循环之外,或者我错误地绑定参数或类似的东西但是代码用于在旧的mysql代码中工作但是我无法在mysqli中使用它(不能因我的PHP版本而使用PDO)
应用程序的网址为here。如果你输入“AAD”,它说它实际上找不到任何结果它应该这样做,因为db中有5行包含术语'AAD'
以下是表单代码:
<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>
以下是整个mysqli代码及其注释
//Get terms entered in the textbox
$questioncontent = (isset($_GET['questioncontent'])) ? $_GET['questioncontent'] : '';
$searchquestion = $questioncontent;
$terms = explode(" ", $searchquestion);
//Start of query
$questionquery = "
SELECT q.QuestionId, q.QuestionContent, o.OptionType, an.Answer, r.ReplyType,
FROM Answer an
INNER JOIN Question q ON q.AnswerId = an.AnswerId
JOIN Reply r ON q.ReplyId = r.ReplyId
JOIN Option_Table o ON q.OptionId = o.OptionId
WHERE ";
$i=0;
//loop through each term
foreach ($terms as &$each) {
$each = '%'.$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 ? ";
}
}
//group terms by terms entered in chronilogical order
$questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY "; $i = 0; foreach ($terms as $each) {
$i++;
//if multiple terms, then display results that contains all terms first, then start displaying other results by knocking off 1 term at a time
if ($i != 1)
$questionquery .= "+";
$questionquery .= "IF(q.QuestionContent LIKE ? ,1,0)";
}
$questionquery .= " DESC ";
//bind the parameters by the amount of terms there are
$types = "";
for($i = 0; $i<sizeof($terms); $i++) {
$types .= "s";
}
//merge the parameters and the terms
$eachterms = array_merge( array($types), $terms);
//prepare quert
$stmt=$mysqli->prepare($questionquery);
//in array call all the parameters and its terms
call_user_func_array(array($stmt, 'bind_param'), $eachterms);
//execute terms
$stmt->execute();
//bind the result
$stmt->bind_result($dbQuestionContent);
//display the results of the successful search
$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;
}
答案 0 :(得分:1)
此错误消息的原因是$stmt
不是MySQLi_Stmt object。原因是您传递给$mysqli->prepare
的语句中存在语法错误。
"… q.QuestionContent LIKE '%'.$each.'%' "
这导致类似:
… q.QuestionContent LIKE '%'.<value of each>.'%'
此处的问题:.
不是MySQL中的字符串连接运算符(您需要使用+
)而$each
可能不是正确引用的MySQL字符串声明,因此它会产生一个有效的表达。
除此之外,将数据直接插入到语句中,会破坏使用预准备语句的主要原因之一:将命令和数据彼此分离,以便数据不会被误解为命令(即SQL注入)。
因此,不要直接插入字符串值,而是放置?
。然后准备语句并bind the parameters给它执行:
$questionquery = "
SELECT q.QuestionId, q.QuestionContent, o.OptionType, an.Answer, r.ReplyType,
FROM Answer an
INNER JOIN Question q ON q.AnswerId = an.AnswerId
JOIN Reply r ON q.ReplyId = r.ReplyId
JOIN Option_Table o ON q.OptionId = o.OptionId
WHERE ";
$paramTypes = '';
$params = array();
$i=0;
//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 ? ";
}
$params[] = "%$each%";
$paramTypes .= "s";
}
//group terms by terms entered in chronilogical order
$questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY ";
$i = 0;
foreach ($terms as $each) {
$i++;
//if multiple terms, then display results that contains all terms first, then start displaying other results by knocking off 1 term at a time
if ($i != 1) $questionquery .= "+";
$questionquery .= "IF(q.QuestionContent LIKE ? ,1,0)";
$params[] = "%$each%";
$paramTypes .= "s"
}
$questionquery .= " DESC ";
//prepare query
$stmt=$mysqli->prepare($questionquery);
//bind parameters
call_user_func_array(array($stmt, 'bind_params'), array_merge(array($paramTypes), $params));
//execute query
$stmt->execute();
哦,再说一遍:你应该看看fulltext search。它可以使整个手动查询构建过时:
… WHERE MATCH (q.QuestionContent) AGAINST ("search terms" IN NATURAL LANGUAGE MODE)
答案 1 :(得分:0)
更改声明:
$questionquery .= "IF(q.QuestionContent LIKE '%'.$each.'%' ,1,0)";
$questionquery .= "q.QuestionContent LIKE '%'.$each.'%' ";
$questionquery .= "OR q.QuestionContent LIKE '%'.$each.'%' ";
到此(分别):
$questionquery .= "IF(q.QuestionContent LIKE '%" . $each . "%' ,1,0)";
$questionquery .= "q.QuestionContent LIKE '%" . $each . "%' ";
$questionquery .= "OR q.QuestionContent LIKE '%" . $each . "%' ";
另外,我认为您需要为$stmt
变量执行此操作:
$stmt = $mysqli->stmt_init();
$stmt->prepare($questionquery);
//execute query
$stmt->execute();