codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.39k stars 1.9k forks source link

Bug: Database Connection ID Is Empty #6403

Closed fre2mansur closed 2 years ago

fre2mansur commented 2 years ago

PHP Version

8.1

CodeIgniter4 Version

4.2.4

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

mysql

What happened?

Connection id is always empty whether is the connection is correct or wrong.

Steps to Reproduce

 $custom = [
  'DSN'      => '',
  'hostname' => 'localhost',
  'username' => 'root1',
  'password' => "",
  'database' => 'my_db_2',
  'DBDriver' => 'MySQLi',
  'DBPrefix' => '',
  'pConnect' => false,
  'DBDebug'  => (ENVIRONMENT !== 'production'),
  'charset'  => 'utf8',
  'DBCollat' => 'utf8_general_ci',
  'swapPre'  => '',
  'encrypt'  => false,
  'compress' => false,
  'strictOn' => false,
  'failover' => [],
  'port'     => '3306',
];
$db = db_connect($custom);
print_r($db);  // [connID] => 
print_r($db->error());  // Attempt to read property "errno" on bool

Expected Output

The connection id should be valid for the connected connection

Anything else?

image

kenjis commented 2 years ago

db_connect() does not connect to the database yet. What do you want to do?

fre2mansur commented 2 years ago

To test the custom connection before do the forge operations

I am trying to this by CI way,

 Create connection $conn = new mysqli($servername, $username, $password);

// Check connection if ($conn->connect_error) {   die("Connection failed: " . $conn->connect_error); } echo "Connected successfully";

fre2mansur commented 2 years ago

db_connect() does not connect to the database yet. What do you want to do?

What do you mean by it does not connected to the database yet?

kenjis commented 2 years ago

What do you mean by it does not connected to the database yet?

db_connect() just creates CI's DB connection object. The code in your usage is not executed yet in the db_connect().

// Create connection
$conn = new mysqli($servername, $username, $password);
paulbalandan commented 2 years ago

You don't have a failover set for your DB connections, so upon db_connect() your connection won't start yet. Try either (1) setting failover connections, or (2) db_connect($custom)->connect();