2014-07-01 43 views
0

我有工作,但查詢不具備綁定值:準備默認查詢

$this->_db->query("SELECT * from posts WHERE post_title LIKE '%$this->search_keywords%' OR post_content LIKE '%$this->search_keywords%' LIMIT 100"); 
$this->rows_results_found = $this->_db->resultset(); 

$this->results_found_num = sizeof($this->rows_results_found); 

我就重寫這一個,但沒有運氣:

$this->_db->query('SELECT * from posts WHERE post_title LIKE :search_keywords OR post_content LIKE :search_keywords LIMIT 100'); 
      $this->_db->bind(':search_keywords', '%$this->search_keywords%'); 

      $this->rows_results_found = $this->_db->resultset(); 

      $this->results_found_num = sizeof($this->rows_results_found); 

$ this-> results_found_num總是顯示爲空數組。

這是我在結果集()(這是從另一個外部類)

public function resultset() { 
     $this->execute(); 
     return $this->stmt->fetchAll(PDO::FETCH_ASSOC); 
    } 

我是缺少在這裏?

預先感謝您!

+0

請RTFM如何使用準備好的語句:http://php.net/manual/ en/pdo.prepared-statements.php – deceze

+0

嗯,只是爲了記錄,我寫了FK手冊。謝謝! :) –

+0

...那麼你應該超過資格回答你自己的問題......?! :) \ * confused \ * – deceze

回答

1

假設你有一個有效的連接調整功能爲以下內容:

public function get_posts($conn) { 
    $query = 'SELECT * 
       FROM posts 
       WHERE post_title LIKE :search_keywords1 OR 
        post_content LIKE :search_keywords2 LIMIT 100'; 
    $stmt = $conn->prepare($query); 
    $stmt->bindValue(':search_keywords1', '%'.$this->search_keywords.'%'); 
    $stmt->bindValue(':search_keywords2', '%'.$this->search_keywords.'%'); 
    $stmt->execute(); 

    return $stmt->fetchAll(PDO::FETCH_ASSOC); 
} 

用法:

$posts = $this->_db->get_posts($conn); 
//var_dump($post) 
$this->rows_results_found = count($post); 
+0

多謝!我只是沒有我的查詢變量,它沒有準備好。現在一切正常!我只是使用$ this - > _ db->而不是$ stmt->但我想這是相同的,因爲我的連接是在另一個類中設置的,這就是爲什麼我使用_db->。再次,thanx! :)這就是我需要的! –