2014-07-01 31 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! :)这就是我需要的! –