rg007891
rg007891

Reputation: 53

In Codeigniter 4, how to display database name and table prefix?

So far in codeigniter 3 we can get database name and table prefix (as mentioned in config/database.php) from the below code lines:

echo $this->db->database;
echo $this->db->dbprefix('emp_table');

Now how can we call these values in Codeigniter 4?

Upvotes: 1

Views: 2347

Answers (3)

JWC May
JWC May

Reputation: 654

There are some changes in CI4, Inside controller's constructor,

write:

$db = \Config\Database::connect();
define('production',$db->database);

Upvotes: 1

Sam
Sam

Reputation: 141

$this->db->database This will work. its Access Modifiers is protected. So this property can be accessed within the class and by classes derived from that class. So to get globally use a public function as

public function get_db_name(){
   return $this->db->database;
}

Upvotes: 0

Chibueze Agwu
Chibueze Agwu

Reputation: 990

I think in Codeigniter 4.0.1 this is what you are looking for


$db = \Config\Database::connect();
$Database = $db->database();
$DBPrefix = $db->getPrefix();

I hope this work for you

Upvotes: 1

Related Questions