【问题标题】:Codeigniter get existing database connection infoCodeigniter 获取现有的数据库连接信息
【发布时间】:2016-06-02 12:41:14
【问题描述】:

我想在我现有的 codeingiter 项目上执行一个后台脚本。

脚本将使用与我的 Codeingiter 应用程序完全相同的用户名、密码、数据库和数据库主机。我想将此信息作为命令参数传递给我的脚本。

让我举个例子(假设我们在一个模型中):

public function callBackgroundScript()
{
   $db_host='';
   $db_name='';
   $db_user='';
   $db_password='';
   exec("php somescript.php $db_host $db_name $db_user $db_password > /dev/null &");
}

在上面的示例中,我将如何使用现有连接设置($this->db)theese 值?

【问题讨论】:

    标签: php codeigniter background codeigniter-3


    【解决方案1】:

    您可以使用get_instance 类。

    $instance = &get_instance();
    $instance->load->database();
    echo $instance->db->hostname;
    

    这是您检索数据库信息的方法。

    【讨论】:

    • 如果我已经加载了一个连接,我不能通过 $this->db->hostname 检索它吗?
    • 是的,如果你已经加载了一个连接,你可以用同样的方式检索。但请记住,您将无法在 helper 中使用 $this。
    猜你喜欢
    • 1970-01-01
    • 2019-10-17
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多