From 84a3f5d4f2fd587da981b040c9fc38d67f612eb4 Mon Sep 17 00:00:00 2001 From: Tolgahan <47753083+tolgahanacar@users.noreply.github.com> Date: Thu, 15 Aug 2024 10:21:04 +0300 Subject: [PATCH] Update connect.php PDO Settings via Array: PDO settings are passed as an array directly in the PDO constructor instead of setAttribute. This makes for shorter, cleaner writing. Exit Function Usage: exit is used instead of die, reflecting a more modern PHP usage. Default Retrieval Mode: PDO::FETCH_OBJ is set as the default retrieval mode so data will be returned as object. This makes your code a little cleaner and makes data manipulation easier (optional). --- controller/connect.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/controller/connect.php b/controller/connect.php index 96425bd..954a052 100644 --- a/controller/connect.php +++ b/controller/connect.php @@ -1,7 +1,10 @@ setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); -}catch(PDOException $e){ - die($e->getMessage()); -} \ No newline at end of file +try { + $db = new PDO("mysql:host=localhost;dbname=rest-api;charset=UTF8", "user", "userpass", [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, + ]); +} catch (PDOException $e) { + exit($e->getMessage()); +} +?>