Skip to content

Commit

Permalink
Update connect.php
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
tolgahanacar authored Aug 15, 2024
1 parent b590b5f commit 84a3f5d
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions controller/connect.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php
try{
$db = new PDO("mysql:host=localhost;dbname=rest-api;charset=UTF8","tolga","123456");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
die($e->getMessage());
}
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());
}
?>

0 comments on commit 84a3f5d

Please sign in to comment.