Reputation: 39
I tried various other threads on this topic, but it did not work. So I write my specific case. I repeat, I have already tried other threads but trying what others advised I did not get results.
So, I'm creating my own little cms, are not very experienced. My structure is this:
/includes/config.php (class config.php)
/index.php
/other.php
Now I want to create an admin area, in a folder:
**/admin**
I would then call the config.php file in order to build queries and everything else, but I can not. I've tried this:
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/config.php';
But it does not work, how can I fix?
EDIT:
includes/config.php
class db {
private $conn;
private $host;
private $user;
private $password;
private $baseName;
private $port;
private $Debug;
private $stmt;
function __construct($params=array()) {
$this->conn = false;
$this->host = 'localhost'; //hostname
$this->user = ''; //username
$this->password = ''; //password
$this->baseName = ''; //name of your database
$this->port = '3306';
$this->debug = true;
$this->connect();
}
function __destruct() {
$this->disconnect();
}
function connect() {
if (!$this->conn) {
try {
$this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->baseName.'', $this->user, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
}
catch (Exception $e) {
die('Error : ' . $e->getMessage());
}
if (!$this->conn) {
$this->status_fatal = true;
echo 'Connection DB failed';
die();
}
else {
$this->status_fatal = false;
}
}
return $this->conn;
}
function disconnect() {
if ($this->conn) {
$this->conn = null;
}
}
public function query($query){
$this->stmt = $this->conn->prepare($query);
}
public function bind($param, $value, $type = null){
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->conn->lastInsertId();
}
public function beginTransaction(){
return $this->conn->beginTransaction();
}
public function endTransaction(){
return $this->conn->commit();
}
public function cancelTransaction(){
return $this->conn->rollBack();
}
public function debugDumpParams(){
return $this->conn->debugDumpParams();
}
}
$database = new db(); $pdo =& $database;
Upvotes: 1
Views: 147
Reputation: 39
the problem was something trivial. In config.php I recall the various classes, then by admin path was wrong and could not pick them up.
EXAMPLE:
include_once 'includes/user.php';
$users = new User($pdo);
$user =& $users;
How can I fix? By adding an autoloading? I've been to but I can not figure out how to use it.. @Millanzor
Upvotes: 0
Reputation: 1930
You can simply go up a folder by using ../includes/config.php if you are coming from the /admin folder, the 2 dots mean you go up 1 directory, followed by the directory 'includes' and then the config.php file.
Another solution can by using an .htaccess file (in case you are using Apache) to route all your requests to 1 file, your index.php file for example. That way you are always coming from the same file and routing your application from there.
Upvotes: 1