Section 2 finished.
This commit is contained in:
parent
13490cdad2
commit
c96119c47f
2 changed files with 12 additions and 4 deletions
|
|
@ -8,9 +8,9 @@ class Database {
|
|||
$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) {
|
||||
public function query($query, $params = []) {
|
||||
$statement = $this->connection->prepare($query);
|
||||
$statement->execute();
|
||||
$statement->execute($params);
|
||||
return $statement;
|
||||
}
|
||||
}
|
||||
12
index.php
12
index.php
|
|
@ -1,11 +1,19 @@
|
|||
<?php
|
||||
require 'functions.php';
|
||||
require 'Database.php';
|
||||
require 'router.php';
|
||||
//require 'router.php';
|
||||
$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);
|
||||
$id = $_GET['id'];
|
||||
// Variante 1
|
||||
$query = "SELECT * FROM posts WHERE id = ?";
|
||||
$posts = $db->query($query, [$id])->fetchAll(PDO::FETCH_ASSOC);
|
||||
// Variante 2
|
||||
//$query = "SELECT * FROM posts WHERE id = :id";
|
||||
//$posts = $db->query($query, [':id' => $id])->fetchAll(PDO::FETCH_ASSOC);
|
||||
// Beide Varianten machen inhaltlich das Gleiche.
|
||||
// freie Wahl, welche der beiden Varianten bevorzugt wird.
|
||||
|
||||
dd($posts);
|
||||
Loading…
Add table
Add a link
Reference in a new issue