我不知道我是否有这个正确但我想要做的是用户输入一个术语,或在文本框中输入多个术语,并在用户提交文本框后,它应该显示包含该术语的任何结果。但我似乎无法让它工作,所以我的问题是,当我在使用mysqli在文本框中输入时能够从数据库中检索术语时,我是否在正确的轨道上?我不确定查询是否与like语句一致,如果它循环遍历每个术语但是如果有人可以帮助它将非常感激:)
我也收到了一个警告,这是需要修复的:
警告:mysqli_stmt :: bind_param()[mysqli-stmt.bind-param]:变量数量与第84行的......中预准备语句中的参数数量不匹配。如何解决这个问题?
以下是代码的mysqli方面:
<?php
$username="xxx";
$password="xxx";
$database="mobile_app";
$mysqli = new mysqli("localhost", $username, $password, $database);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$questioncontent = (isset($_GET['questioncontent'])) ? $_GET['questioncontent'] : '';
?>
<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 = array(explode(" ", $searchquestion));
//loop through each term
foreach ($terms as &$each) {
$each = '%'.$each.'%';
$questionquery = "
SELECT q.QuestionContent
FROM Question q
WHERE ";
$i=0;
$whereArray = array();
$orderByArray = array();
$orderBySQL = "";
$paramString = "";
//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 ? ";
$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);;
$stmt->bind_param($paramString, array_merge($whereArray, $orderByArray));
$stmt->execute();
$stmt->bind_result($dbQuestionContent);
$questionnum = $stmt->num_rows();
}
?>
答案 0 :(得分:0)
这应该做你想要的大部分,我想 - 我没有测试过,所以可能会有错别字;我会把这些作为练习给读者留下来。
$terms = array(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); ;
$stmt->bind_param($paramString, array_merge($whereArray, $orderByArray));