prabhat rai
prabhat rai

Reputation: 21

Including Config file in connection file not working

I'm creating a CMS that provides dynamic database creation at the initial project setup stage.

I have defined the database configuration details as constants in config.php file:

//Database Name
define('DEFAULT_DB_NAME', 'cms');

//User Name
define('DEFAULT_USER_NAME', 'root');

//Password
define('DEFAULT_PASSWORD', '');

//Host Name
define('DEFAULT_HOST_NAME', 'localhost');

I included the config.php file in my database connection file (db.class.php):

<?php
include_once '../../config/config.php';

class Db{
    protected $conn;
    protected $host = DEFAULT_HOST_NAME;
    protected $username = DEFAULT_USER_NAME;
    protected $password = DEFAULT_PASSWORD;
    protected $dbname = DEFAULT_DB_NAME;

    public function __construct(){
        $this->conn = new mysqli($this->host, $this->username, $this->password, $this->dbname);

        if($this->conn->connect_error){
            die("<h3>Connection Failed!!! Error: " . $this->conn->connect_error . "</h3>");
        }
    }
}

I also have created a dynamic Style sheet (admintheme.php) that modifies the admin panel based on user preference:

<?php
    header("Content-type: text/css;");

    include_once '../../model/admintheme.class.php';

    $theme = new Admintheme();
    $result = $theme->ReadAdminTheme();

    if($result == '' || $result == '0'){
        $sidebarBg = "#111";
        $sidebarPosition = "left";
        $sidebarunset = "left";
        $sidebarright = "unset";
    }
    else{
        $row = $result->fetch_assoc();
        
        $sidebarBg = $row['sidebarbg'];
        $sidebarPosition = $row['sidebar_position'];

        if($sidebarPosition == "left"){
            $sidebarunset = "right";
        }
        else{
            $sidebarunset = "left";
        }
    }
?>
/*-- ------------------------xx----------------------- */

/***** Content Section Starts *****/
.content{
    margin-<?= $sidebarPosition; ?>: auto;
}
/***** Content Section Ends *****/


/**** Side Bar Section Starts *****/
.sidebar-nav{
    background-color: <?= $sidebarBg; ?>;
    <?= $sidebarPosition; ?>: 0px;
}

The issue is that when I use static data in "db.class.php" file (ex. directly write "localhost" in place of the constant and so on..) then "admintheme.php" works fine and displays the desired output, but when I use constants in place of the static data then all other functionalities of the project work fine and retrieve data from the database except "admintheme.php".

The "admintheme.php" also works fine when the constants are defined inside the "db.class.php" or if the "config.php" file is in the same directory as of the "db.class.php" but while including the the "config.php" file from other directory, all other data is retrieved except "admintheme.php".

**No direct error is received but in the browser's console, it states (include_once(../../config/config.php): failed to open stream: No such file or directory...)

**Included path is correct in both "db.class.php" and "admintheme.php".

Any help is greatly appreciated.

Upvotes: 0

Views: 519

Answers (1)

prabhat rai
prabhat rai

Reputation: 21

Adding "DIR" magic constant solved the issue.

Upvotes: 0

Related Questions