0

我不知道我是否正确,但我想做的是用户在文本框中输入一个术语或多个术语,并且在用户提交文本框后,它应该显示任何结果包含该术语。但我似乎无法让它工作,所以我的问题是,当我在文本框中输入时使用 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();
}           
            ?>
4

1 回答 1

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));
于 2012-06-26T15:14:50.487 回答