我是 php/OOP 的初学者,并且有一个关于动态更改我的数据库连接的问题。
这是我的 main.php 的样子:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends 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
文件名:控制器/main.php
行号:12 遇到错误
您尚未选择要连接的数据库类型。
更改$this->load->database($connectdb)
为default
,default1
或default2
使其工作。如何在其中放置一个变量,以便连接参数根据谁登录而改变?
希望有人能帮忙,谢谢!埃里克