CodeIgniter 是否支持PDO查询?给个例子

WBOY
Release: 2016-06-06 20:41:16
Original
1235 people have browsed it

配置CodeIgniter为PDO 类型,但是怎么都查询不到数据,却可以查处有多少记录。

代码

仔细跟踪代码,发现有如下一句:

<code>#文件 /system/database/drivers/pdo/pdo_driver.php
    function _execute($sql)
    {
        $sql = $this->_prep_query($sql);
        $result_id = $this->conn_id->prepare($sql);

        if (is_object($result_id) && $result_id->execute())
        {
            if (is_numeric(stripos($sql, 'SELECT')))
            {
                $this->affect_rows = count($result_id->fetchAll());
            }
            else
            {
                $this->affect_rows = $result_id->rowCount();
            }
        }
        else
        {
            $this->affect_rows = 0;
            return FALSE;
        }

        return $result_id;
    }


</code>
Copy after login
Copy after login

问题

<code>$this->affect_rows = count($result_id->fetchAll());
</code>
Copy after login
Copy after login

这里的CI使用 $result_id->fetchAll() 来统计符合条件的个数,但是这个 fetchAll() 执行一次之后就不能再得到数据。所以导致后期就无法获得数据。

测试

<code><?php //怎么都不会有结果,但是却会有条数
class User_model extends CI_Model {
    public function getList(){    
        $sql = "select * from #@__user ";
        return $this->db->query($sql)->result_array();
    }
}
</code>
Copy after login
Copy after login
<code>//这样写就会有结果记录,但是默认的 $this->db->query($sql) .. 却又成摆设了!
class User_model extends CI_Model{
    public function getList(){
        $sql = "select * from ci_user";
        $stmt = $this->db->conn_id->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }
}
</code>
Copy after login
Copy after login

应该是不支持,但是官网下载的里面却带有PDO包。
想确认的是,codeigniter是否真的支持PDO?希望能够给个例子!

回复内容:

配置CodeIgniter为PDO 类型,但是怎么都查询不到数据,却可以查处有多少记录。

代码

仔细跟踪代码,发现有如下一句:

<code>#文件 /system/database/drivers/pdo/pdo_driver.php
    function _execute($sql)
    {
        $sql = $this->_prep_query($sql);
        $result_id = $this->conn_id->prepare($sql);

        if (is_object($result_id) && $result_id->execute())
        {
            if (is_numeric(stripos($sql, 'SELECT')))
            {
                $this->affect_rows = count($result_id->fetchAll());
            }
            else
            {
                $this->affect_rows = $result_id->rowCount();
            }
        }
        else
        {
            $this->affect_rows = 0;
            return FALSE;
        }

        return $result_id;
    }


</code>
Copy after login
Copy after login

问题

<code>$this->affect_rows = count($result_id->fetchAll());
</code>
Copy after login
Copy after login

这里的CI使用 $result_id->fetchAll() 来统计符合条件的个数,但是这个 fetchAll() 执行一次之后就不能再得到数据。所以导致后期就无法获得数据。

测试

<code><?php //怎么都不会有结果,但是却会有条数
class User_model extends CI_Model {
    public function getList(){    
        $sql = "select * from #@__user ";
        return $this->db->query($sql)->result_array();
    }
}
</code>
Copy after login
Copy after login
<code>//这样写就会有结果记录,但是默认的 $this->db->query($sql) .. 却又成摆设了!
class User_model extends CI_Model{
    public function getList(){
        $sql = "select * from ci_user";
        $stmt = $this->db->conn_id->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }
}
</code>
Copy after login
Copy after login

应该是不支持,但是官网下载的里面却带有PDO包。
想确认的是,codeigniter是否真的支持PDO?希望能够给个例子!

可以拓展下driver

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template