2012-06-26 30 views
0

方面,我不知道我有這個正確的,但我所試圖做的是,用戶在長期進入,或者在一個文本框和後多條款用戶提交文本框,它應顯示包含該詞的任何結果。但我似乎無法得到它的工作,所以我的問題是,當我在正確的軌道上使用mysqli能夠從文本框中輸入數據庫時​​檢索條款?我不知道查詢是否正確與類似的聲明,如果它循環通過每個術語,但如果任何人都可以幫助它將不勝感激:)如何使用mysqli的從數據庫中獲取

我收到警告以及這是需要固定:

警告: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(); 
}   
      ?> 
+0

您是否嘗試過回顯SQL查詢以查看它的外觀? – andrewsi

+0

當我回顯查詢時,它始終顯示:SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE?或q.QuestionContent LIKE? GROUP BY q.QuestionId,q.SessionId ORDER BY + IF(q.QuestionContent LIKE ?, 1,0)DESC'。我不認爲這是正確的,因爲讓我們說你只輸入一個詞,它應該是這個'SELECT q.QuestionContent FROM Question q WHERE q.QuestionContent LIKE? GROUP BY q.QuestionId,q.SessionId ORDER BY + IF(q.QuestionContent樣的?,1,0)DESC'但如果你再在3個方面,例如它應該改變這個... – user1394925

+0

進入'選擇q.QuestionContent FROM問題q WHERE q.QuestionContent LIKE?或q.QuestionContent LIKE?或q.QuestionContent LIKE? GROUP BY q.QuestionId,q.SessionId ORDER BY + IF(q.QuestionContent樣的?,1,0)DESC' OR語句需要改變自身取決於已經在文本框中 – user1394925

回答

0

這應該做你最想要的東西,我想 - 我沒有測試過,所以有可能是拼寫錯誤;我會留下修復這些作爲讀者的練習。

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

在聊天我已經發布了它在你的代碼 – user1394925

+0

顯示問題我更新了有問題的代碼,以便您能夠看到代碼在代碼中的外觀 – user1394925

+0

試一下 - 設置循環中數組變量的行實際上是將它們轉換爲字符串。 – andrewsi

相關問題