CodeIgniter PDO database driver not working

前端 未结 4 789
梦如初夏
梦如初夏 2021-01-04 09:35

I\'m trying to use the PDO MySQL driver in my CodeIgniter application. This is my database config:

$active_group = \'default\';
$active_record = TRUE;

$db[\         


        
相关标签:
4条回答
  • 2021-01-04 09:53

    According to CodeIgniter's Database Configuration page,

    For the PDO driver, you should change the 'hostname => 'localhost'' to "'hostname' => mysql:host=localhost" like below:

    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'mysql:host=localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'database_name',
        'dbdriver' => 'pdo',
        'dbprefix' => '',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
         .....
         .....
    );
    
    0 讨论(0)
  • 2021-01-04 09:54

    This should not be the case.

    localhost;dbname=testdatabase
    

    should be

    mysql:dbname=testdatabase;host=localhost;
    
    0 讨论(0)
  • 2021-01-04 09:54

    On file /application/config/database.php where is

    $db['default']['hostname'] = 'localhost';
    

    must be

    $db['default']['hostname'] = 'mysql:host=localhost';
    

    localhost or your database host.

    0 讨论(0)
  • 2021-01-04 10:06

    PDO driver require a full DSN string to be provided. The string like this

    'dsn' = ‘mysql:host=localhost;dbname=databasename’;
    

    when you use this string you should remove host and databasename value from array. I think following example gives you an idea.

    $db['default'] = array(
    'dsn'   => 'mysql:host=localhost;dbname=codeigniter3',
    'hostname' => '',
    'username' => 'root',
    'password' => '',
    'database' => '',
    'dbdriver' => 'pdo',
    'dbprefix' => '',
    'pconnect' => FALSE,
    '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
    );
    

    Thanks

    0 讨论(0)
提交回复
热议问题