CodeIgniter Forums
Having trouble connecting to mysql database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Having trouble connecting to mysql database (/showthread.php?tid=77920)



Having trouble connecting to mysql database - vazaia - 11-04-2020

Hello,

I want to connect MySQL community server, but I keep getting this error message over and over:8

CodeIgniter\Database\Exceptions\DatabaseException #8

Unable to connect to the database.


This is the code:

$custom = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'world',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'    => 3306,
];

$db = \Config\Database::connect($custom);
$db->query("SELECT * FROM city");
$db->close();


Any kind of help would be appreciated



RE: Having trouble connecting to mysql database - InsiteFX - 11-04-2020

And where are you calling the bottom database code?

Database code should be called from a model.