如何使用 MySQLi 从数据库中检索术语


how to use mysqli to retrieve terms from database

我不知道

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

我认为这应该可以满足您的大部分需求 - 我没有测试过它,所以可能有错别字;我将把修复这些作为读者的练习留给读者。

$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));