php动态连接数据库,php-Codeigniter动态数据库连接

我是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("Employees");

echo "
";

print("Visitors");

}//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-> load->数据库($connectdb)更改为’default’,’default1’或’default2’使其工作.如何在其中放置变量,以便连接参数根据谁登录而更改?

希望有人可以帮助,谢谢!

埃里克

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值