dongru2019 2016-05-20 13:42
浏览 39
已采纳

PHP需要root文件

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条回答 默认 最新

  • douying3251 2016-05-20 14:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗