Summary Files Commits Branches @commit @diff @stats
path: root / src / models / DB.php
diff options
context:
space:
mode:
Commit852e91a3da52a478c942bf7a664df8d8798b0641 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-04-01 10:26:26 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-04-01 10:26:26 +0300
Filesa0701de5463fa8ae769b8ba6d5bde98d82d1608b /src/models/DB.php
devrix-job-board-852e91a3da52a478c942bf7a664df8d8798b0641.tar
devrix-job-board-852e91a3da52a478c942bf7a664df8d8798b0641.tar.gz
devrix-job-board-852e91a3da52a478c942bf7a664df8d8798b0641.zip
Parent30ee8530a646d3dcf32d563bbf42475035449bb5 (diff)
Implemented delete from DB and added error handling to queries
1 files changed, 36 insertions, 6 deletions
-rw-r--r--src/models/DB.php42
diff --git a/src/models/DB.php b/src/models/DB.php
index 4c75d17..b287273 100644
--- a/src/models/DB.php
+++ b/src/models/DB.php
@@ -12,8 +12,13 @@ class DB {
private PDO $connection;
function __construct() {
- $this->connection = new PDO("mysql:host=" . self::$servername, self::$username, self::$password);
- $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ try {
+ $this->connection = new PDO("mysql:host=" . self::$servername, self::$username, self::$password);
+ $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
// Make sure the actual db exists
$createDBQuery = "CREATE DATABASE IF NOT EXISTS " . self::$dbname . ";";
@@ -36,7 +41,12 @@ class DB {
$query .= "PRIMARY KEY (id));";
- $this->connection->exec($query);
+ try {
+ $this->connection->exec($query);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
}
function insertValue(string $tableName, ...$values) {
@@ -54,7 +64,12 @@ class DB {
$query .= ");";
- $this->connection->exec($query);
+ try {
+ $this->connection->exec($query);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
}
/* Read */
@@ -84,8 +99,23 @@ class DB {
$query = rtrim($query, ", ");
$query .= " WHERE id = {$id};";
- echo $query;
- $this->connection->exec($query);
+ try {
+ $this->connection->exec($query);
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
+ }
+
+ /* Delete */
+
+ function deleteById(string $tableName, int $id) {
+ try {
+ $this->connection->exec("DELETE FROM {$tableName} WHERE id = {$id}");
+ }
+ catch (PDOException $e) {
+ echo "[DB Exception]: " . $e->getMessage();
+ }
}
}