douyan6742 2014-07-01 14:22
浏览 21
已采纳

准备默认查询,结果为带有绑定的准备查询

I have a query that is working but is does not have binded values:

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

I am rewriting it to this one but with no luck:

$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 appears always to be an empty array.

This is what I have in the resultset() (It is from another outer class):

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

What I am missing here?

Thank you in advance!

  • 写回答

1条回答 默认 最新

  • doudao2954 2014-07-01 15:45
    关注

    Assuming you have a valid connection adjust your function to the following:

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

    Usage:

    $posts = $this->_db->get_posts($conn);
    //var_dump($post)
    $this->rows_results_found = count($post);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line