2012-06-28 37 views
1

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

    } 


?> 
+0

這是什麼意思:'LIKE?' –

+0

@MattK:'?'是一個mysqli佔位符,用於使用'bind_param'綁定參數。 – chaos

+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條款是絕對什麼都不做有用的,對不對?

+0

我忘了把「DESC」放在ORDER BY的末尾,這就是爲什麼,我會測試你的代碼並回復你:),謝謝 – user1394925

+0

好的努力,但沒有運氣,仍然無法顯示一個詞的成功搜索,查詢和參數的輸出是目前如果試圖搜索一個單詞:'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'它仍然顯示消息'抱歉,沒有發現這個搜索問題'這應該只顯示如果沒有行被發現,但它應該能夠找到一些行在mySQL數據庫中測試 – user1394925

+0

我將更新有問題的代碼,所以你現在可以看到什麼它看起來像 – user1394925

相關問題