ci3使用PDO连接数据库
$db['default'] = array(
'dsn' => "mysql:host=localhost;dbname=test;charset=utf8",//设置数据库连接字符串
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'test',
'dbdriver' => 'pdo', //修改数据库驱动
'dbprefix' => 'c_',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
function show()
{
$pdo = $this->db->connect(); //建立对数据库的连接。
$pdo = $this->db->db_pconnect(true); //建立对数据库的连接,使用持久连接
$sql = "select * from c_news";
$res = $pdo->query($sql);
$lst = $res->fetchAll();
p($lst);
}