Welcome Guest, Not a member yet? Register   Sign In
Cant connect to to DB using CI4
#1

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' => [],
];
Reply
#2

Is port missing?

PHP Code:
        'port'     => 3306
Reply
#3

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.

CodeIgniter Wizard (CRUD code generator for Mac) instantly scaffolds Bootstrap-based web applications with an administrative interface (admin templates include Bootstrap5)

Reply
#4

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
];
Reply
#5

(This post was last modified: 03-17-2021, 12:27 PM by InsiteFX.)

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 03-17-2021, 01:43 PM by eldan88.)

(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?
Reply
#7

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

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

https://phpdelusions.net/
Reply
#9

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

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB