2

我是 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,default1default2使其工作。如何在其中放置一个变量,以便连接参数根据谁登录而改变?

希望有人能帮忙,谢谢!埃里克

4

2 回答 2

4

您应该提供所有数据库信息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

于 2013-10-02T17:17:58.253 回答
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";
}
于 2020-05-20T11:18:25.233 回答