Connect to database from another file class
I am trying to connect to a database PDO, but am having problems doing so.
I have tried to putt the connection inside the class.acl.php
The error is Fatal error: Call to a member function prepare() on a
non-object in.... class.acl.php
Connect.php:
$config = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'lar'
);
$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' .
$config['dbname'], $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
class.acl.php:
class ACL{
private $db;
function __constructor($userID=''){
if ($userID != ''){
$this->userID = floatval($userID);
}else{
$this->userID = floatval($_SESSION['userID']);
}
$this->userRoles = $this->getUserRoles('ids');
$this->buildACL();
}
function ACL($userID=''){
$this->__constructor($userID);
}
function database($database){
$this->db = $database;
}
function getPermKeyFromID($permID){
Code....
return $permID
}
}
index.php:
require 'class.acl.php';
$userID=$_SESSION['ID'];
$ACL = new ACL($userID);
$ACL->database($db);
echo $ACL->getPermKeyFromID('1');
include "database_connector.php";
ReplyDeleteclass Controller
{
protected $con;
public function __construct(){
$dc = new DatabaseConnector();
$this->con = $dc->getConnection();
}
public function insertData()
{
$name = "Abcde";
$sql = "insert into user_details values('".$name."')";
mysqli_query($this->con, $sql);
}
}
khanhvuongtuan1@gmail.com - Working at ComponentPro