Codeigniter动态数据库连接

时间:2013-10-02 17:03:37

标签: php mysql database codeigniter dynamic

我是php / OOP的初学者,对于动态更改数据库连接有疑问。

这是我的main.php的样子:

类Main扩展了CI_Controller {

function __construct()
{

    parent::__construct();

    /* Standard Libraries of codeigniter are required */
    $this->load->database($connectdb);
    $this->load->helper('url');
    $this->load->library('grocery_CRUD');
}

public function index()
{


            if ($_POST["username"] == "root")
                    {
                            $connectdb="default";
                    }


                    if ($_POST["username"] == "user1")
                    {
                            $connectdb="default1";
                    }

            if ($_POST["username"] == "user2")
                    {
                            $connectdb="default2";
                    }


    $connect = @mysql_connect("localhost", $_POST["username"], $_POST["password"]);//won't display the warning if any.
    if (!$connect)
    {
            echo 'Server error. Please try again sometime. CON';
    }else{
            print("<a href=\"http://v-admindb/ci/index.php/main/employees?username=".$_POST["username"]."\">Employees</a>");
            echo "<br>";
            print("<a href=\"http://v-admindb/ci/index.php/main/visitors?username=".$_POST["username"]."\">Visitors</a>");
    }//Just an example to ensure that we get into the function
// LOAD LIBRARIES
}

public function employees()
{
    $this->grocery_crud->set_table('employees');
    $output = $this->grocery_crud->render();
    $this->_example_output($output);
}

public function visitors()
{
    $this->grocery_crud->set_table('visitors');
    $output = $this->grocery_crud->render();
    $this->_example_output($output);
}


function _example_output($output = null)

{
    $this->load->view('our_template.php',$output);
}

}

这是我的database.php:

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'my_new_cms';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;


//CONNECTION FOR user1
$db['default1']['hostname'] = 'localhost';
$db['default1']['username'] = 'user1';
$db['default1']['password'] = 'user1';
$db['default1']['database'] = 'my_new_cms';
$db['default1']['dbdriver'] = 'mysql';
$db['default1']['dbprefix'] = '';
$db['default1']['pconnect'] = TRUE;
$db['default1']['db_debug'] = TRUE;
$db['default1']['cache_on'] = FALSE;
$db['default1']['cachedir'] = '';
$db['default1']['char_set'] = 'utf8';
$db['default1']['dbcollat'] = 'utf8_general_ci';
$db['default1']['swap_pre'] = '';
$db['default1']['autoinit'] = TRUE;
$db['default1']['stricton'] = FALSE;


//CONNECTION FOR user2
$db['default2']['hostname'] = 'localhost';
$db['default2']['username'] = 'user2';
$db['default2']['password'] = 'user2';
$db['default2']['database'] = 'my_new_cms';
$db['default2']['dbdriver'] = 'mysql';
$db['default2']['dbprefix'] = '';
$db['default2']['pconnect'] = TRUE;
$db['default2']['db_debug'] = TRUE;
$db['default2']['cache_on'] = FALSE;
$db['default2']['cachedir'] = '';
$db['default2']['char_set'] = 'utf8';
$db['default2']['dbcollat'] = 'utf8_general_ci';
$db['default2']['swap_pre'] = '';
$db['default2']['autoinit'] = TRUE;
$db['default2']['stricton'] = FALSE;

当我尝试登录时,我可以出现此错误:

遇到PHP错误

严重性:注意

消息:未定义的变量:connectdb

文件名:controllers / main.php

行号:12 遇到错误

您尚未选择要连接的数据库类型。

将$ this-&gt; load-&gt;数据库($ connectdb)更改为'default','default1'或'default2'使其正常工作。如何在其中放置变量,以便连接参数根据登录的人员而变化?

希望有人能提供帮助,谢谢! 埃里克

2 个答案:

答案 0 :(得分:3)

您应该在application/config/database.php´

中提供所有数据库信息

通常,您可以设置默认数据库组,如下所示:

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

请注意,登录信息和设置在名为$db['default']的数组中提供。

然后,您可以在新数组中添加另一个数据库 - 让我们称之为'anotherdb'。

$db['anotherdb']['hostname'] = "localhost";
$db['anotherdb']['username'] = "root";
$db['anotherdb']['password'] = "";
$db['anotherdb']['database'] = "another_database_name";
$db['anotherdb']['dbdriver'] = "mysql";
$db['anotherdb']['dbprefix'] = "";
$db['anotherdb']['pconnect'] = TRUE;
$db['anotherdb']['db_debug'] = FALSE;
$db['anotherdb']['cache_on'] = FALSE;
$db['anotherdb']['cachedir'] = "";
$db['anotherdb']['char_set'] = "utf8";
$db['anotherdb']['dbcollat'] = "utf8_general_ci";
$db['anotherdb']['swap_pre'] = "";
$db['anotherdb']['autoinit'] = TRUE;
$db['anotherdb']['stricton'] = FALSE;

现在,如果您想使用第二个数据库,请转到

$DB_another = $this->load->database('anotherdb', TRUE); 

然后,您将$this->db->foo()

而不是$DB_another->foo()

你可以将它扩展到像这样的多个组

 $DB2 = $this->load->database('anotherdb1', TRUE); 
 $DB3 = $this->load->database('anotherdb2', TRUE); 

有关详细信息,请查看此处: http://ellislab.com/codeigniter/user-guide/database/connecting.html

答案 1 :(得分:0)

$this->load->database($connectdb);

问题出在这里,您将此变量加载到在索引函数中设置的构造函数中。

__ constructor()在index()之前被触发,所以我想您应该执行以下操作:

class Main extends CI_Controller {
private $connectdb;

function __construct()
{

    parent::__construct();

    /* Standard Libraries of codeigniter are required */
    $this->_setConnectdb();
    $this->load->database($this->connectdb);
    $this->load->helper('url');
    $this->load->library('grocery_CRUD');
}

public function _setConnectdb()
{
if ( $_POST["username"] == "root" ) {
    $this->connectdb = "default";
}

if ( $_POST["username"] == "user1" ) {
    $this->connectdb = "default1";
}

if ( $_POST["username"] == "user2" ) {
    $this->connectdb = "default2";
}