Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带结果的已准备默认查询到带绑定的已准备查询_Php_Pdo - Fatal编程技术网

Php 带结果的已准备默认查询到带绑定的已准备查询

Php 带结果的已准备默认查询到带绑定的已准备查询,php,pdo,Php,Pdo,我有一个正在工作但没有绑定值的查询: $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-&

我有一个正在工作但没有绑定值的查询:

$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\u found\u num始终显示为空数组

这是我在resultset()中看到的内容(它来自另一个外部类):

我错过了什么


提前谢谢你

假设您有一个有效的连接,请将您的功能调整为以下内容:

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);

请RTFM如何使用准备好的声明:嗯,只是为了记录,我写了Fk手册。谢谢你!:)。。。那么你应该更有资格回答自己的问题……?!:)*困惑*至少我们在这里丢失了大量代码,没有足够的信息说明问题是什么或到底发生了什么。太多了!我只是没有我的查询变量,它也没有准备好。现在一切都好了!我只是使用$this->\u db->而不是$stmt->,但我认为这是相同的,因为我的连接是在另一个类中设置的,这就是我使用\u db->的原因。再次,thanx!:)这就是我需要的!
$posts = $this->_db->get_posts($conn);
//var_dump($post)
$this->rows_results_found = count($post);