Summary Files Commits Branches @commit @diff @stats
path: root / src / models / DB.php
diff options
context:
space:
mode:
Commit30ee8530a646d3dcf32d563bbf42475035449bb5 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-04-01 10:13:17 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-04-01 10:13:17 +0300
Filescb81dab896f16e71bbfab27d633785fa4278aecb /src/models/DB.php
devrix-job-board-30ee8530a646d3dcf32d563bbf42475035449bb5.tar
devrix-job-board-30ee8530a646d3dcf32d563bbf42475035449bb5.tar.gz
devrix-job-board-30ee8530a646d3dcf32d563bbf42475035449bb5.zip
Parentf9e2090a1028712ecc6e1b0c71390c821689b414 (diff)
Implemented update from DB and updated some comments and phpdocumentator tags
1 files changed, 30 insertions, 10 deletions
-rw-r--r--src/models/DB.php40
diff --git a/src/models/DB.php b/src/models/DB.php
index b973e4b..4c75d17 100644
--- a/src/models/DB.php
+++ b/src/models/DB.php
@@ -24,6 +24,8 @@ class DB {
$this->connection->exec("USE " . self::$dbname);
}
+ /* Create */
+
function createTable(string $tableName, array $columns) {
$query = "CREATE TABLE IF NOT EXISTS {$tableName}(
id INT NOT NULL AUTO_INCREMENT,";
@@ -37,6 +39,26 @@ class DB {
$this->connection->exec($query);
}
+ function insertValue(string $tableName, ...$values) {
+ $query = "INSERT INTO {$tableName} VALUES(NULL, ";
+
+ foreach($values as $value) {
+ if (gettype($value) == "string") {
+ $query .= "'{$value}', ";
+ }
+ else {
+ $query .= "{$value}, ";
+ }
+ }
+ $query = rtrim($query, ", ");
+
+ $query .= ");";
+
+ $this->connection->exec($query);
+ }
+
+ /* Read */
+
function getById(string $tableName, int $id) {
$sth = $this->connection->prepare("SELECT * FROM `{$tableName}` WHERE id = {$id}");
$sth->execute();
@@ -51,20 +73,18 @@ class DB {
return $sth->fetchAll(PDO::FETCH_OBJ);
}
- function insertValue(string $tableName, ...$values) {
- $query = "INSERT INTO {$tableName} VALUES(NULL, ";
+ /* Update */
- foreach($values as $value) {
- if (gettype($value) == "string") {
- $query .= "'{$value}', ";
- }
- else {
- $query .= "{$value}, ";
- }
+ function updateById(string $tableName, int $id, ...$values) {
+ $query = "UPDATE {$tableName} SET ";
+
+ for ($i = 0; $i < sizeof($values); $i += 2) {
+ $query .= "{$values[$i]} = " . (gettype($values[$i+1]) == "string" ? "'{$values[$i+1]}'" : $values[$i+1]) . ", ";
}
$query = rtrim($query, ", ");
- $query .= ");";
+ $query .= " WHERE id = {$id};";
+ echo $query;
$this->connection->exec($query);
}