1

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;

2
  • ../includes/config.php from the /admin/ Commented May 20, 2016 at 13:42
  • No, the .. means you are coming back to previous folder, for you that is parent folder, so you can access the config file. Commented May 20, 2016 at 13:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting the suspicion that perhaps my class has any broken link, for the files in the root is called correctly as well as all classes in it, but if as in my example, I find myself in the admin folder, can not recall everything correctly. I'm posting the code of the file config.php
What errors are you getting? Your config.php does not seem to contain any problems.
The page does not connect, HTTP ERROR 500.
That seems like you have error reporting off. Can you either enable error reporting in PHP by putting error_reporting(E_ALL); at the top of your file or do a quick Google on the reporting, there are a few things you might need to enable it. Otherwise, your server probably has an error.log showing you the exact thing that went wrong here.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.