Does CodeIgniter support PDO queries? Give an example

Source: Internet
Author: User
Configuration CodeIgniter is the PDO type, but how can not query the data, but to investigate how many records.

Code

Follow the code carefully and find the following sentence:

#文件 /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;    }

Problem

$this->affect_rows = count($result_id->fetchAll());

The CI here uses $result _id->fetchall () to count the number of matches, but the fetchall () cannot get the data once it is executed once. This results in the inability to obtain data later.

Test


  
   db->query($sql)->result_array();    }}
//这样写就会有结果记录,但是默认的 $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();    }}

Should not be supported, but the official website download with PDO package.
Do you want to confirm that CodeIgniter really supports PDO? Hope to give an example!

Reply content:

Configuration CodeIgniter is the PDO type, but how can not query the data, but to investigate how many records.

Code

Follow the code carefully and find the following sentence:

#文件 /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;    }

Problem

$this->affect_rows = count($result_id->fetchAll());

The CI here uses $result _id->fetchall () to count the number of matches, but the fetchall () cannot get the data once it is executed once. This results in the inability to obtain data later.

Test


  
   db->query($sql)->result_array();    }}
//这样写就会有结果记录,但是默认的 $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();    }}

Should not be supported, but the official website download with PDO package.
Do you want to confirm that CodeIgniter really supports PDO? Hope to give an example!

Can expand the next driver

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.