首页 > 解决方案 > 在 CodeIgniter 3 中使用 PDO 进行查询

问题描述

我只是尝试使用 CI V3.1.9 连接到 MySQL 服务器并检索数据。使用PDO作为驱动怎么样?

如何获取数据?因为我的查询总是返回空格。

这是我的database.php

'dsn'   => 'mysql:host=localhost;dbname:db_dms;',
'hostname' => '',
'username' => 'user',
'password' => '123',
'database' => 'db_dms',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE

这是我的controller.php

class Login extends CI_Controller
{
    public function index()
    {
        $data['bError'] = false;
        $data['sErrorMessage'] = '';

        $this->load->model("ModPengguna");

        if(isset($_POST['btnLogin']))
        {
            $result = $this->db->conn_id->prepare("select * from m_user where userID = ? and pass = ?");

            //binding option using PDO
            $result->bindValue(1, 'SUPERADM01', PDO::PARAM_STR);
            $result->bindValue(2, '123', PDO::PARAM_STR);
            $result->execute();
            print_r($result->result());

            //I also tried this
            print_r($result->fetch(PDO::FETCH_ASSOC));
        }
    }
}

如何获取数据?

谢谢。

标签: phpcodeigniterpdocodeigniter-3

解决方案


推荐阅读