我的查询正在运行,但没有绑定值:
$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 :(得分: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);