Home PHP pass values from php file to other class
Reply: 0

PHP pass values from php file to other class

user1744 Published in May 20, 2018, 9:56 am

I am using a config.php file for a simular something, im trying to put my mysql credentials in there to then use them in a different file, but it does not pass the values,

is there any1 that could help me find a solution.

code config.php:

/* Database credentials*/
$dbHost = 'localhost';
$dbName = 'xx';
$dbUsername = 'xx';
$dbWachtwoord = 'xx'; 

code dbconnect.php:

<?php include 'config.php';
class Database
    private $host;
    private $db_name;
    private $username;
    private $password;
    public $conn;

    public function dbConnection()
        $this->host = $dbHost;
        $this->db_name = $dbName;
        $this->username = $dbUsername;
        $this->password = $dbWachtwoord;
        $this->conn = null;
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        catch(PDOException $exception)
            echo "Connection error: " . $exception->getMessage();

        return $this->conn;

Class.user dbconnection:


class USER

    private $conn;

    public function __construct()
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;

    public function runQuery($sql)
        $stmt = $this->conn->prepare($sql);
        return $stmt;

Thanks in advance =)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308461 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO