【问题标题】:how to use mysqli to retrieve terms from database如何使用 mysqli 从数据库中检索术语
【发布时间】:2012-06-26 11:55:15
【问题描述】:

我不知道我是否正确,但我想做的是用户在文本框中输入一个术语或多个术语,并且在用户提交文本框后,它应该显示包含该术语的任何结果。但我似乎无法让它工作,所以我的问题是,当我使用 mysqli 在文本框中输入时能够从数据库中检索术语时,我是否走在正确的轨道上?我不确定查询是否与 like 语句正确,是否循环遍历每个术语,但如果有人能提供帮助,将不胜感激:)

我也收到了警告,这是需要修复的:

警告:mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of variables does not match number of parameters in Prepared statement in ... on line 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();
}           
            ?>

【问题讨论】:

  • 您是否尝试过回显您的 SQL 查询以查看它的样子?
  • 当我回显查询时,它始终显示:SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE ? OR q.QuestionContent LIKE ? GROUP BY q.QuestionId, q.SessionId ORDER BY +IF(q.QuestionContent LIKE ? ,1,0) DESC。我认为这不正确,因为假设您只输入 1 个术语,应该是这个 SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE ? GROUP BY q.QuestionId, q.SessionId ORDER BY +IF(q.QuestionContent LIKE ? ,1,0) DESC 但是如果你然后输入 3 个术语,例如它应该更改为这个......
  • SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE ? OR q.QuestionContent LIKE ? OR q.QuestionContent LIKE ? GROUP BY q.QuestionId, q.SessionId ORDER BY +IF(q.QuestionContent LIKE ? ,1,0) DESC OR 语句需要根据文本框中输入的术语数量自行更改
  • 那你需要打勾,统计搜索框中输入的词条的个数,多于一个才加上OR语句。
  • 此外,您需要为包含的每个绑定参数调用bindParam - SQL 中的每个? 都需要一个;因此您需要确保以正确的顺序传递正确的参数。

标签: php mysqli


【解决方案1】:

我认为这应该可以满足您的大部分需求-我尚未对其进行测试,因此可能存在拼写错误;我将把修复这些作为练习留给读者。

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

【讨论】:

  • 在聊天中我发布了一个问题,它显示在您的代码上
  • 我也更新了有问题的代码,这样您就可以看到您的代码在我的代码中的样子
  • 试一试——在循环中设置数组变量的行实际上是将它们转换为字符串。
  • 好的,这两个警告已经解决了。剩下一个警告说明:Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of elements in type definition string doesn't match number of bind variables in ... on line 88 问题是当我回显查询时,它仍然没有显示多个术语的 OR 语句。它呼应了这个SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE ? GROUP BY q.QuestionId, q.SessionId ORDER BY IF(q.QuestionContent LIKE ? ,1,0)
  • 问题是,如果我输入了一个正确的词,它仍然说这个词没有结果,但它确实应该找到结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
  • 2018-01-06
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 2019-05-30
  • 2020-08-03
相关资源
最近更新 更多