Part on Enviroments finished

This commit is contained in:
Patryk Hegenberg 2022-12-13 18:04:25 +01:00
parent 57abb48e63
commit 8c7ac2df36
3 changed files with 17 additions and 7 deletions

View file

@ -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);

10
config.php Normal file
View file

@ -0,0 +1,10 @@
<?php
return [
'database' => [
'host' => 'localhost',
'port' => 3306,
'dbname' => 'myapp',
'charset' => 'utf8mb4'
]
];

View file

@ -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);