CodeIgniter Forums
Cant connect to to DB using CI4 - 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: Cant connect to to DB using CI4 (/showthread.php?tid=78837)

Pages: 1 2


Cant connect to to DB using CI4 - eldan88 - 03-16-2021

Hi,

I have been using CI3 for couple years. I am installing CI4 as a fresh installation. When i enter my DB credentials on my localhost I keep getting a error message Unable to connect to the database. Below is my config for my database. Any help would be really appreciated.

public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'online_order',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => TRUE,
'DBDebug' => TRUE,
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'strictOn' => FALSE,
'failover' => [],
];


RE: Cant connect to to DB using CI4 - kenjis - 03-16-2021

Is port missing?

PHP Code:
        'port'     => 3306



RE: Cant connect to to DB using CI4 - gosocial2 - 03-17-2021

If your port is already 3306 and you're still having the problem, then try changing 'hostname' => 'localhost' to 'hostname' => '127.0.0.1'.

Make sure these values are not overridden with different values by corresponding settings in the .env file.


RE: Cant connect to to DB using CI4 - eldan88 - 03-17-2021

Hi,

I have used the following config but its still not connecting ...


public $default = [
'DSN' => '',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => 'root',
'database' => 'online_ordering_new',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => TRUE,
'DBDebug' => TRUE,
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'strictOn' => FALSE,
'failover' => [],
'port' => 3309
];


RE: Cant connect to to DB using CI4 - InsiteFX - 03-17-2021

Try testing it with MySQLi Test

PHP Code:
<?php

/**
 * Fill our vars and run on cli - $ php -f mysqli_test.php
 * or
 * Use php editor save file as mysqli_test.php
 */

$dbname 'database_name'// The database name
$dbuser 'user_name';     // The users name
$dbpass 'user_password'// The database users password
$dbhost 'database_host'// usally localhost else use 27.0.0.1

// Try to connect
$connect mysqli_connect($dbhost$dbuser$dbpass) or die("Unable to Connect to '$dbhost'");

// Select the database
mysqli_select_db($connect$dbname) or die("Could not open the db '$dbname'");

// Test query to get database tables
$test_query "SHOW TABLES FROM $dbname";

// The query result
$result mysqli_query($connect$test_query);

$tblCnt 0;

while(
$tbl mysqli_fetch_array($result))
{
    
$tblCnt++;
    echo 
$tbl[0]."<br>\n";
}

if ( ! 
$tblCnt)
{
    echo "There are no tables<br>\n";
}
else
{
    echo 
"There are $tblCnt tables<br>\n";


See if that works if not you have a problem in user password or host.


RE: Cant connect to to DB using CI4 - eldan88 - 03-17-2021

(03-17-2021, 12:26 PM)InsiteFX Wrote: Try testing it with MySQLi Test

PHP Code:
<?php

/**
 * Fill our vars and run on cli - $ php -f mysqli_test.php
 * or
 * Use php editor save file as mysqli_test.php
 */

$dbname 'database_name'// The database name
$dbuser 'user_name';     // The users name
$dbpass 'user_password'// The database users password
$dbhost 'database_host'// usally localhost else use 27.0.0.1

// Try to connect
$connect mysqli_connect($dbhost$dbuser$dbpass) or die("Unable to Connect to '$dbhost'");

// Select the database
mysqli_select_db($connect$dbname) or die("Could not open the db '$dbname'");

// Test query to get database tables
$test_query "SHOW TABLES FROM $dbname";

// The query result
$result mysqli_query($connect$test_query);

$tblCnt 0;

while(
$tbl mysqli_fetch_array($result))
{
 
$tblCnt++;
 echo 
$tbl[0]."<br>\n";
}

if ( ! 
$tblCnt)
{
    echo "There are no tables<br>\n";
}
else
{
 echo 
"There are $tblCnt tables<br>\n";


See if that works if not you have a problem in user password or host.

It working fine and showing all the tables. Also my table is working fine with my old CI3 . Do you think it has something to do with spark? Because I am using MAMP as my local dev server.

So i realized it is because of php spark. When i just go on localhost/codedeniter/public/home it works fine. Any reason why?


RE: Cant connect to to DB using CI4 - InsiteFX - 03-17-2021

For one set the pConnect to false it causes problems.

If that doe's not work post your complete Database.php file so we can look at it.

I use xampp and I can connect fine but I do not use spark.


RE: Cant connect to to DB using CI4 - John_Betong - 03-17-2021

Checkout the following site and use their recommended MySqli error validation when connecting. It is comprehensive.

https://phpdelusions.net/


RE: Cant connect to to DB using CI4 - kenjis - 03-17-2021

(03-17-2021, 10:48 AM)eldan88 Wrote:         'port' => 3309
It seems MySQL port is 3306.
Is that your typo?


RE: Cant connect to to DB using CI4 - eldan88 - 03-17-2021

(03-17-2021, 03:44 PM)kenjis Wrote:
(03-17-2021, 10:48 AM)eldan88 Wrote:         'port' => 3309
It seems MySQL port is 3306.
Is that your typo?

Yes but its still not working with php spark