Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何动态获取多个数据库连接?_Php_Codeigniter_Database Connection - Fatal编程技术网

Php 如何动态获取多个数据库连接?

Php 如何动态获取多个数据库连接?,php,codeigniter,database-connection,Php,Codeigniter,Database Connection,我需要3个与Codeigniter的数据库连接。我不知道关于这些数据库的主机、用户名或密码的任何信息。这意味着用户必须使用表单发送这些信息 我所知道的是,可以在config/database.php中添加另一个数组,比如$db['otheran_db'],但是我如何动态设置主机名、用户名和密码的值呢 我尝试使用自己的类来完成此操作,但我没有真正工作: <?php class Connection_model extends CI_Model { public function de

我需要3个与Codeigniter的数据库连接。我不知道关于这些数据库的主机、用户名或密码的任何信息。这意味着用户必须使用表单发送这些信息

我所知道的是,可以在config/database.php中添加另一个数组,比如$db['otheran_db'],但是我如何动态设置主机名、用户名和密码的值呢

我尝试使用自己的类来完成此操作,但我没有真正工作:

<?php
class Connection_model extends CI_Model {
    public function define_database($member, $host, $user, $pw, $database) {
        $db[$member] = array(
                'dsn'   => '',
                'hostname' => $host,
                'username' => $user,
                'password' => $pw,
                'database' => $database,
                'dbdriver' => 'mysqli',
                'dbprefix' => '',
                'pconnect' => FALSE,
                'db_debug' => TRUE,
                'cache_on' => FALSE,
                'cachedir' => '',
                'char_set' => 'utf8',
                'dbcollat' => 'utf8_general_ci',
                'swap_pre' => '',
                'autoinit' => TRUE,
                'encrypt' => FALSE,
                'compress' => FALSE,
                'stricton' => FALSE,
                'failover' => array(),
                'save_queries' => TRUE
        );
    }

    public function get_person($member, $host, $user, $pw) {
        $otherdb = $this->load->database($this->define_database($member, $host, $user, $pw, $database), TRUE);

        $query = $otherdb->select('first_name, last_name')->get('person');
        var_dump($query);
    }
}
我得到消息:未定义变量:数据库


我做错了什么?或者有其他方法获得另一个连接吗?

看看你的get_person方法,它需要4个参数:

public function get_person(**$member, $host, $user, $pw**) 
$this->define_database($member, $host, $user, $pw, **$database**)
但在这里面,您调用方法define_database传递5个参数:

public function get_person(**$member, $host, $user, $pw**) 
$this->define_database($member, $host, $user, $pw, **$database**)

CI给出了错误,因为第五个参数未在此范围内定义。

噢,多么愚蠢的错误。谢谢,我更改了它,但现在我遇到另一个错误:您尚未选择要连接的数据库类型。那么如何选择那个数据库呢?我想问题是:$otherdb=$this->load->database($this->define_database($member,$host,$user,$pw,$database),TRUE);您的define_数据库方法不返回任何内容,因此load->database take null参数尝试写入:return$db[$member];在define_数据库方法的末尾,您是对的!我将return$db[$member]添加到我的define_数据库函数中,它现在可以工作了。非常感谢你!