[英]Access database config variables from a helper in Codeigniter
是否可以从Codeigniter中的帮助程序获取database.php变量值?
Here is the way, normally you won't be able to use $this
in helper, so you have to use get_instance()
. 这是方法,通常您将无法在辅助程序中使用
$this
,因此必须使用get_instance()
。 I have given an example of 'hostname' you can use the config name you need. 我给出了“主机名”的示例,您可以使用所需的配置名称。
function test()
{
$CI =& get_instance();
$CI->load->database();
echo $CI->db->hostname; // give the config name here (hostname).
}
$ci=& get_instance();
$ci->config->load('database');
$ci->config->item('item name');
If you want to access the config file for the database when $this->config->load(); 如果要在$ this-> config-> load();时访问数据库的配置文件,请执行以下操作: is not available , the solution could look like this:
不可用 ,解决方案如下所示:
include(APPPATH.'config/database'.EXT);
$conn = mysql_connect($db['default']['hostname'], $db['default']['username'], $db['default']['password']);
mysql_select_db($db['default']['database'], $conn);
My easiest fix was 我最简单的解决方法是
include_once APPPATH . 'config/database.php';
echo json_encode($db); // contains all the database configurations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.