我试图根据谁登录到网页中动态设置数据库连接凭证。我很确定它不工作,因为$connectdb
变量没有被定义。有人可以看看我的代码,并试图让它工作吗?谢谢!PHP - 全局变量
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
$connectdb="";
class Main extends CI_Controller {
function __construct() {
parent::__construct();
echo $connectdb;
$this->load->database($connectdb);
$this->load->helper('url');
$this->load->library('grocery_CRUD');
}
public function index() {
if ($_POST["username"] == "root") {
global $connectdb="default";
}
if ($_POST["username"] == "user1") {
global $connectdb="user1";
}
if ($_POST["username"] == "user2") {
global $connectdb="user2";
}
$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);
}
}
这不会令人遗憾。我需要做的是根据登录屏幕上的用户名(使用$ _POST [“username”])设置一个名为$ connectdb的变量,然后使用$ this-> load-> database($ connectdb)我试着列出多个if语句在构造函数内但它不起作用。 – user2839568
我现在在你的代码中看到你可能正在使用[grocery crud](http://www.grocerycrud.com)。我继续前进,花8秒钟为您搜索[google](https://www.google.com/search?q=grocery+crud+multiple+databases):http://www.grocerycrud.com/forums /主题/ 420-回答灿我交换机的数据库/ – stormdrain