What is PDO database configuration in CodeIgniter?

2019-06-11 13:39发布

问题:

In application/config/database.phpfor setting up a optimal database configuration should set the $db['default']['hostname'] = ''; to 'localhost' by use of mysql driver.

But when we use PDO driver for this configuration and localhost for the host name we see ERROR. What is PDO database hostname configuration in CodeIgniter with PDO driver shown below and what should place instead of question mark?

$db['default']['hostname'] = '?????';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'XXX';
$db['default']['dbdriver'] = 'PDO';

回答1:

If your trying to connect pdo try in codeigniter 3

$db['default'] = array(
'dsn'  => 'mysql:host=localhost; dbname=myproject; charset=utf8;',
'hostname' => 'localhost',
'username' => 'root',
'password' => '*********',
'database' => '',
'dbdriver' => 'pdo',


回答2:

For the PDO driver, you should use the $config['dsn'] setting instead of hostname and database:

// PDO
$db['default']['dsn'] = 'pgsql:host=localhost;port=5432;dbname=database_name';

Documentations:

http://www.codeigniter.com/user_guide/database/connecting.html?highlight=pdo

http://www.codeigniter.com/user_guide/database/configuration.html