Skip to content

Commit 84a3f5d

Browse files
authored
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).
1 parent b590b5f commit 84a3f5d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

controller/connect.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
2-
try{
3-
$db = new PDO("mysql:host=localhost;dbname=rest-api;charset=UTF8","tolga","123456");
4-
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
5-
}catch(PDOException $e){
6-
die($e->getMessage());
7-
}
2+
try {
3+
$db = new PDO("mysql:host=localhost;dbname=rest-api;charset=UTF8", "user", "userpass", [
4+
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
5+
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
6+
]);
7+
} catch (PDOException $e) {
8+
exit($e->getMessage());
9+
}
10+
?>

0 commit comments

Comments
 (0)