Part on Enviroments finished
This commit is contained in:
parent
57abb48e63
commit
8c7ac2df36
3 changed files with 17 additions and 7 deletions
|
|
@ -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
10
config.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'database' => [
|
||||
'host' => 'localhost',
|
||||
'port' => 3306,
|
||||
'dbname' => 'myapp',
|
||||
'charset' => 'utf8mb4'
|
||||
]
|
||||
];
|
||||
|
|
@ -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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue