I set up the following code for my DB-Connection, however I cannot create a connection to my database from another .php file (in another directory):
My database.php file:
<?php
ini_set('display_errors', 'On');
public class Database {
public function __construct() {
$this->dsn = 'mysql:host=xxx;dbname=xxx';
$this->username = 'xxx';
$this->password = 'xxx';
}
public function __construct($dsn, $username, $password) {
$this->dsn = $dsn;
$this->username = $username;
$this->password = $password;
}
public function db_connect() {
try {
$database = new PDO($this->dsn , $this->username, $this->password);
return $database;
} catch(PDOException $e) {
echo $e->getMessage();
}
}
public function run_query($database, $query){
try {
$result = $database->prepare($query);
$result->execute();
return $result;
} catch (Exception $e) {
echo $e->getMessage();
die();
}
}
}
?>
The directory of this file is currentdirectory/php/database.php.
I am trying to instantiate a Database connection in another file (named page.php) with the following code:
include("php/database.php")
$database = new Database();
$connection = $database->db_connect();
$result = $database->run_query($connection, $query);
The directory of this file is currentdirectory/page.php.
I have been searching for an error quite a while now and cannot see what I did wrong. The other questions regarding PDO-DB classes didn't help me much further either. Thanks in advance for any help!