简体   繁体   English

Codeigniter 4、如何显示数据库名和表前缀?

[英]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:到目前为止,在 codeigniter 3 中,我们可以从以下代码行获取数据库名称和表前缀(如config/database.php中所述)

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

Now how can we call these values in Codeigniter 4?现在我们如何在 Codeigniter 4 中调用这些值?

I think this is what you are looking for我想这就是你要找的


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

I hope this work for you我希望这对你有用

There are some changes in CI4, Inside controller's constructor, CI4里面有一些变化,里面控制器的构造函数,

write:写:

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

And inside your function call it:在您的 function 内部调用它:

echo(production);

Never call database as a function () in CI4, avoid this () and just use the object instead: database without ()切勿在 CI4 中将数据库称为 function (),避免此 (),而只需使用 object 代替:没有 () 的数据库

$this->db->database This will work. $this->db->database这会起作用。 its Access Modifiers is protected .它的访问修饰符是protected的。 So this property can be accessed within the class and by classes derived from that class.因此,可以在 class 内以及从 class 派生的类访问此属性。 So to get globally use a public function as因此,要在全球范围内使用public function 作为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM