为什么我不能成功搜索

时间:2012-06-28 16:58:17

标签: php mysqli

我在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;

  }


?> 

1 个答案:

答案 0 :(得分:0)

看起来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条款绝对没有用,对吗?