I am trying to pass a variable into $this->load->database($connectdb)
The idea is that a user will log into a webpage and based on the user their database credentials will be located in database.php. The variable $connectdb will contain the correct login info so that the user can only access specified tables in the database. Here is my code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database($connectdb);
$this->load->helper('url');
$this->load->library('grocery_CRUD');
}
public function index()
{
if ($_POST["username"] == "root")
{
$connectdb="root";
}
if ($_POST["username"] == "user1")
{
$connectdb="user1";
}
if ($_POST["username"] == "user2")
{
$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);
}
}
When I try to login I get the error:
Severity: Notice
Message: Undefined variable: connectdb
Filename: controllers/main.php
Line Number: 12
You have not selected a database type to connect to.
I'm guessing this has to do with variable scopes but I'm not sure. I am very new at php/oop in general so any help you can give would be appreciated!
Thanks Eric