CodeIgniter Forums
Unable to connect to the database. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Unable to connect to the database. (/showthread.php?tid=77263)

Pages: 1 2


Unable to connect to the database. - shivek sahay - 08-09-2020

Hii, I have just started learning CI4 (moving from CI3 to CI4).

I just followed the tutorial given in the codeigniter official website link- https://codeigniter.com/user_guide/tutorial/index.html


But after making a connection with the database i was facing the unable to connect the database error
and i have checked the connection it is valid but it is not getting connected in CI4 only

error image in attachment.

Please Help Me to resolve the error my env file is also in attachment.


RE: Unable to connect to the database. - captain-sensible - 08-09-2020

there are a few questions that come to mind. I used to use MySQl but quite honestly find sqlite3 easier and since CI4 has a public dir, then my database is outside whats accessible from the web. Sqlite3 is server-less which means you can play with Ci4 and sqlite3on the desktop.

Now a few things come to mind; where on your PC are you running CI4 apache, xampp or other. You don't say you set up a user and password on the database with appropriate grant permissions so i can't guess that you have done that. Obviously if you didn't do that then you won't get a connection to the database.


I mostly don't bother with the env and just edit /Config/Database.php


RE: Unable to connect to the database. - shivek sahay - 08-10-2020

Hii captain-sensible and everyone,
My PC configurations are:-

OS - UBUNTU 20.04
PHP Version 7.4.8

Yes i am using Xampp and i have correctly set up the user privileges,as it is working fine for my codeigniter3 projects.

I have edited in Config/Database.php but still the error is there.
Then today i tried some new approach i did the same process in my windows machine and it worked fine.
Then i thought i might have written something wrong in my Ubuntu machine so i made a GitHub repo of the project.
But again i cloned it in Ubuntu machine it started showing the same error.

Do anyone have any idea that why is it giving error. Please HELP !!!


RE: Unable to connect to the database. - paulbalandan - 08-10-2020

Set your logs threshold to 4 so that errors will be logged. Then try running it again. Go to your logs then tell us the error message.


RE: Unable to connect to the database. - devbisco - 08-10-2020

i had same problem


RE: Unable to connect to the database. - shivek sahay - 08-11-2020

Hii i have changed the threshold value to 4 and created a log file of today's . I am adding it below
Code:
Code:
CRITICAL - 2020-08-11 10:53:58 --> Unable to connect to the database.

#0 /opt/lampp/htdocs/ignite4/system/Database/BaseConnection.php(646): CodeIgniter\Database\BaseConnection->initialize()

#1 /opt/lampp/htdocs/ignite4/system/Database/BaseBuilder.php(1860): CodeIgniter\Database\BaseConnection->query()

#2 /opt/lampp/htdocs/ignite4/system/Model.php(462): CodeIgniter\Database\BaseBuilder->get()

#3 /opt/lampp/htdocs/ignite4/app/Models/NewsModel.php(12): CodeIgniter\Model->findAll()

#4 /opt/lampp/htdocs/ignite4/app/Controllers/News.php(13): App\Models\NewsModel->getNews()

#5 /opt/lampp/htdocs/ignite4/system/CodeIgniter.php(918): App\Controllers\News->index()

#6 /opt/lampp/htdocs/ignite4/system/CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController()

#7 /opt/lampp/htdocs/ignite4/system/CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest()

#8 /opt/lampp/htdocs/ignite4/public/index.php(45): CodeIgniter\CodeIgniter->run()

#9 /opt/lampp/htdocs/ignite4/system/Commands/Server/rewrite.php(34): require_once('/opt/lampp/htdo...')

#10 {main}

ERROR - 2020-08-11 10:55:33 --> Error connecting to the database:
Call to undefined function CodeIgniter\Database\MySQLi\mysqli_init()

CRITICAL - 2020-08-11 10:55:33 --> Unable to connect to the database.

#0 /opt/lampp/htdocs/ignite4/system/Database/BaseConnection.php(646): CodeIgniter\Database\BaseConnection->initialize()

#1 /opt/lampp/htdocs/ignite4/system/Database/BaseBuilder.php(1860): CodeIgniter\Database\BaseConnection->query()

#2 /opt/lampp/htdocs/ignite4/system/Model.php(462): CodeIgniter\Database\BaseBuilder->get()

#3 /opt/lampp/htdocs/ignite4/app/Models/NewsModel.php(12): CodeIgniter\Model->findAll()

#4 /opt/lampp/htdocs/ignite4/app/Controllers/News.php(13): App\Models\NewsModel->getNews()

#5 /opt/lampp/htdocs/ignite4/system/CodeIgniter.php(918): App\Controllers\News->index()

#6 /opt/lampp/htdocs/ignite4/system/CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController()

#7 /opt/lampp/htdocs/ignite4/system/CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest()

#8 /opt/lampp/htdocs/ignite4/public/index.php(45): CodeIgniter\CodeIgniter->run()

#9 /opt/lampp/htdocs/ignite4/system/Commands/Server/rewrite.php(34): require_once('/opt/lampp/htdo...')

#10 {main}



RE: Unable to connect to the database. - captain-sensible - 08-12-2020

one thing about xampp if i understand it correctly is that a lot of stuff is embedded for use so its not necessarily all the same as your native PC.

I use apache web server on slackware

Anything that might be of interest in the phpinfo() of xampp ?

A quick google came up that this is not a Codeigniter issue which is backed up by as you say the same stuff working Windows. F
First thing that came to mind is permissions but..whats your approach to editing stuff as a normal user? since xampp is in /opt and therefore root system ?

So whats your app/Config/database.php settings ?


these are mine for sqlite :


Code:
public $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => '',
        'password' => '',
        'database' => ROOTPATH.'writable/Art',
        'DBDriver' => 'SQLite3',
        '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,
    ];



RE: Unable to connect to the database. - jreklund - 08-12-2020

As it says Call to undefined function CodeIgniter\Database\MySQLi\mysqli_init() you haven't enabled mysqli.

Find your active php.ini and make sure it's on.
Code:
extension=php_mysqli.dll



RE: Unable to connect to the database. - captain-sensible - 08-12-2020

extension=php_mysqli.dll is that not windows though ? and hes saying he has an issue on ubuntu linux ? at 3#


RE: Unable to connect to the database. - InsiteFX - 08-12-2020

Check your apache log files

drive\xampp\apache\logs