phpcodeigniterdatabase-connectionconfig

CodeIgniter error: Unable to connect to your database server using the provided settings


I got this error:

Unable to connect to your database server using the provided settings.

Filename: C:/xampp/htdocs/soheil/system/database/DB_driver.php

Line Number: 436

but when I add the below code inside database.php it prints okay:

echo '<pre>';
    print_r($db['default']);
echo '</pre>';

echo 'Connecting to database: ' . $db['default']['database'];
$dbh = mysql_connect
(
    $db['default']['hostname'],
    $db['default']['username'],
    $db['default']['password'])
or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ($db['default']['database']);

echo '<br />   Connected OK:'  ;
die( 'file: ' . __FILE__ . ' Line: ' .__LINE__);

my config:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => '',
    'password' => '',
    'database' => 'soheil',
    '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
);

Solution

  • Are you put your username on database? It maybe root user

    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'soheil',
        '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
    );
    

    Hope this help.