From 8c7ac2df367e432924cf8263564a736199301d71 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Tue, 13 Dec 2022 18:04:25 +0100 Subject: [PATCH] Part on Enviroments finished --- Database.php | 8 +++----- config.php | 10 ++++++++++ index.php | 6 ++++-- 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 config.php diff --git a/Database.php b/Database.php index 8c95c86..5e76ce3 100644 --- a/Database.php +++ b/Database.php @@ -3,12 +3,10 @@ class Database { public $connection; - public function __construct() + public function __construct($config, $username, $password) { - $dsn = "mysql:host=localhost;port=3306;dbname=myapp;charset=utf8mb4"; - $username = 'appUser'; - $password = 'password'; - $this->connection = new PDO($dsn, $username, $password); + $dsn = 'mysql:'.http_build_query($config, '', ';'); + $this->connection = new PDO($dsn, $username, $password, [PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC]); } public function query($query) { $statement = $this->connection->prepare($query); diff --git a/config.php b/config.php new file mode 100644 index 0000000..a344aca --- /dev/null +++ b/config.php @@ -0,0 +1,10 @@ + [ + 'host' => 'localhost', + 'port' => 3306, + 'dbname' => 'myapp', + 'charset' => 'utf8mb4' + ] +]; \ No newline at end of file diff --git a/index.php b/index.php index a050fb7..264b989 100644 --- a/index.php +++ b/index.php @@ -2,8 +2,10 @@ require 'functions.php'; require 'Database.php'; //require 'router.php'; - -$db = new Database(); +$username = 'appUser'; +$password = 'password'; +$config = require('config.php'); +$db = new Database($config['database'], $username, $password); $posts = $db->query("SELECT * FROM posts")->fetchAll(PDO::FETCH_ASSOC); dd($posts); \ No newline at end of file