【问题标题】:Why can I not get a successful search为什么我无法成功搜索
【发布时间】:2012-06-28 16:58:17
【问题描述】:

我在 MYSQLI 的查询中遇到了 LIKE 语句的问题。似乎没有找到任何可以进入 LIKE 语句的术语。我正在尝试将参数绑定到 LIKE 语句中,但我的问题是我是否在 mysqli 中错误地设置了 LIKE 语句?

奇怪的是,如果我输出查询并将其插入 SQL,只需更改“?”对于诸如“%AAB%”之类的术语,它在 SQL 中运行良好,因为它确实显示了包含术语 AAB 的记录。问题是它在 PHP、mysqli 中不起作用,因为如果我在文本框中输入 AAB,它根本不会显示任何记录。它一直说“对不起,没有从搜索中找到问题”。 有人知道这是为什么吗?

下面是代码(我已经使用mysqli连接到db):

<form action="previousquestions.php" method="get">
<p>Search: <input type="text" name="questioncontent" value="<?php echo isset($_GET['questioncontent']) ? $_GET['questioncontent'] : ''; ?>" onchange="return trim(this)" /></p>
      <p><input id="searchquestion" name="searchQuestion" type="submit" value="Search" /></p>
      </form>


<?php 

if (isset($_GET['searchQuestion'])) {

$searchquestion = $_GET['questioncontent'];
$terms = 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++;
    $whereArray[] = $each;
    $orderByArray[] = $each; 
    $paramString .= 'ss';
    //if only 1 term entered then perform this LIKE statement
    if ($i == 1){
        $questionquery .= "q.QuestionContent LIKE CONCAT('%', ?, '%') ";
    } else {
        //If more than 1 term then add an OR statement
        $questionquery .= "OR q.QuestionContent LIKE CONCAT('%', ?, '%') ";
        $orderBySQL .= ",";
    }

    $orderBySQL .= "IF(q.QuestionContent LIKE CONCAT('%', ?, '%') ,1,0)";

}  

$questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY " . $orderBySQL;
$questionquery .= " DESC ";
$stmt=$mysqli->prepare($questionquery)or die($mysqli->error); 

function makeValuesReferenced(&$arr){ 
    $refs = array(); 
    foreach($arr as $key => $value) 
        $refs[$key] = &$arr[$key]; 
    return $refs; 

}

$ref = array_merge((array)$paramString, $whereArray, $orderByArray);
call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($ref));


    $stmt->execute();
    $stmt->bind_result($dbQuestionContent); 
    $questionnum = $stmt->num_rows();

        if($questionnum ==0){
    echo "<p>Sorry, No Questions were found from this Search</p>";
    }
    else{

      $output = "";
        while ($stmt->fetch()) {
$output .= "
      <tr>
      <td class='questiontd'>$dbQuestionContent</td>
      </tr>";
        }
        $output .= "        </table>";

        echo $output;

  }


?> 

【问题讨论】:

  • 这是什么意思:LIKE ?
  • @MattK: ? 是一个 mysqli 占位符,用于使用 bind_param 绑定参数。
  • 啊,我。学到了新东西!
  • 顺便说一句,您在表单输入中未能htmlspecialchars() 您的$_GET['questioncontent'] 是一个XSS 漏洞。 en.wikipedia.org/wiki/Cross-site_scripting

标签: php mysqli


【解决方案1】:

看起来 mysqli 正在转义您的 % 字符,或者作为客户端/服务器通信的一部分正在发生类似的事情。试试这个:

foreach ($terms as $each) {
    $i++;
    $whereArray[] = $each;
    $orderByArray[] = $each; 
    $paramString .= 'ss';
    //if only 1 term entered then perform this LIKE statement
    if ($i == 1){
        $questionquery .= "q.QuestionContent LIKE CONCAT('%', ?, '%') ";
    } else {
        //If more than 1 term then add an OR statement
        $questionquery .= "OR q.QuestionContent LIKE CONCAT('%', ?, '%') ";
        $orderBySQL .= ",";
    }
    $orderBySQL .= "IF(q.QuestionContent LIKE CONCAT('%', ?, '%') ,1,0)"; 
}

您确实意识到您的 ORDER BY 子句完全没有任何用处,对吧?

【讨论】:

  • 我忘了在 ORDER BY 的末尾加上“DESC”,所以我会测试你的代码并回复你 :),谢谢
  • 努力但没有运气,仍然无法显示成功搜索一个词,如果尝试搜索单个词,查询和参数的输出当前是这样的:SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE CONCAT('%', ?, '%') GROUP BY q.QuestionId, q.SessionId ORDER BY IF(q.QuestionContent LIKE CONCAT('%', ?, '%') ,1,0) DESC ss 和它仍在显示消息Sorry, No Questions were found from this Search,该消息仅在未找到任何行时才会出现,但它应该能够找到在 mySQL 数据库中测试的某些行
  • 我将更新有问题的代码,以便您现在可以看到它的样子
  • @user1394925:ss 真的出现在查询的末尾吗?如果是这样,我希望它会产生 SQL 语法错误。
  • 不,我回应了查询“$questionquery”和参数“$paramString”。他们是完全分开的,我只是互相呼应了他们nxt
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-07
相关资源
最近更新 更多