From 0806411f092a7f8dfca5a0bb3ccd89b3e2ff8c6a Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Tue, 13 Dec 2022 08:50:22 +0100 Subject: [PATCH] Part on PDO first steps finished --- index.php | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/index.php b/index.php index 15234bf..cd8c56a 100644 --- a/index.php +++ b/index.php @@ -1,26 +1,18 @@ 'controllers/index.php', - '/about' => 'controllers/about.php', - '/contact' => 'controllers/contact.php', - '/mission' => 'controllers/mission.php', -]; +//require 'router.php'; +// connect to MySQL database. +$dsn = "mysql:host=localhost;port=3306;dbname=myapp;charset=utf8mb4"; +$username = 'appUser'; +$password = 'password'; +$pdo = new PDO($dsn, $username, $password); -function routeToController($uri, $routes) { - if (array_key_exists($uri, $routes)) { - require $routes[$uri]; - } else { - abort(); - } -} +$statement = $pdo->prepare("SELECT * FROM posts"); +$statement->execute(); -routeToController($uri, $routes); +$posts = $statement->fetchAll(PDO::FETCH_ASSOC); -function abort($code = 404) { - http_response_code($code); - require "views/{$code}.php"; - die(); +foreach ($posts as $post) { + echo "
  • " . $post['title'] . "
  • "; } \ No newline at end of file