CI 3 的資料庫設定方式,一般直覺的情況下會是直接在 /application/config/database.php 來進行設定,以下特別針對一些情況來進行記錄:
.mysqli 連線
$db['default'] = array('dsn' => '','hostname' => 'dbhost-domain_or_ip','username' => 'dbuser','password' => 'dbpass','database' => 'dbname','dbdriver' => 'mysqli','dbprefix' => '','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);
.pdo(dsn connection)
$db['default'] = array('dsn' => 'mysql:dbhost-domain_or_ip;dbname=dbname','hostname' => '','username' => 'dbuser','password' => 'dbpass','database' => '','dbdriver' => 'pdo','dbprefix' => '','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);
.另外定義(就是不在database.php中定義的,直接在model中另外寫的)
$pdo = $this->load->database('pdo://dbuser:dbpass@dbhost-domain_or_ip/?dsn=mysql:host=dbhost-domain_or_ip;dbname=dbname&char_set=utf8&dbcollat=utf8_general_ci', true)
$search = $pdo->conn_id->prepare('sql query');
$search->execute();
$result = $search->fetchAll();
特此記錄一下…