Updated the proostekrant to work properly again.
This commit is contained in:
Executable
+98
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class Database {
|
||||
private static $instance = null;
|
||||
private $connection;
|
||||
|
||||
private function __construct() {
|
||||
$config = include __DIR__ . '/../../config/database.php';
|
||||
|
||||
$dsn = "mysql:host={$config['host']};dbname={$config['database']};charset={$config['charset']}";
|
||||
|
||||
try {
|
||||
$this->connection = new PDO(
|
||||
$dsn,
|
||||
$config['username'],
|
||||
$config['password'],
|
||||
[
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]
|
||||
);
|
||||
} catch (PDOException $e) {
|
||||
die("Database connection failed: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function getInstance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function getConnection() {
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
public function query($sql, $params = []) {
|
||||
$stmt = $this->connection->prepare($sql);
|
||||
$stmt->execute($params);
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
public function fetchAll($sql, $params = []) {
|
||||
return $this->query($sql, $params)->fetchAll();
|
||||
}
|
||||
|
||||
public function fetch($sql, $params = []) {
|
||||
return $this->query($sql, $params)->fetch();
|
||||
}
|
||||
|
||||
public function insert($table, $data) {
|
||||
$columns = implode(', ', array_keys($data));
|
||||
$placeholders = ':' . implode(', :', array_keys($data));
|
||||
|
||||
$sql = "INSERT INTO {$table} ({$columns}) VALUES ({$placeholders})";
|
||||
$stmt = $this->connection->prepare($sql);
|
||||
$stmt->execute($data);
|
||||
|
||||
return $this->connection->lastInsertId();
|
||||
}
|
||||
|
||||
public function update($table, $data, $where) {
|
||||
$setParts = [];
|
||||
foreach ($data as $key => $value) {
|
||||
$setParts[] = "{$key} = :{$key}";
|
||||
}
|
||||
|
||||
$whereParts = [];
|
||||
foreach ($where as $key => $value) {
|
||||
$whereParts[] = "{$key} = :where_{$key}";
|
||||
$data["where_{$key}"] = $value;
|
||||
}
|
||||
|
||||
$sql = "UPDATE {$table} SET " . implode(', ', $setParts) .
|
||||
" WHERE " . implode(' AND ', $whereParts);
|
||||
|
||||
$stmt = $this->connection->prepare($sql);
|
||||
return $stmt->execute($data);
|
||||
}
|
||||
|
||||
public function delete($table, $where) {
|
||||
$whereParts = [];
|
||||
$params = [];
|
||||
foreach ($where as $key => $value) {
|
||||
$whereParts[] = "{$key} = :{$key}";
|
||||
$params[$key] = $value;
|
||||
}
|
||||
|
||||
$sql = "DELETE FROM {$table} WHERE " . implode(' AND ', $whereParts);
|
||||
return $this->query($sql, $params)->rowCount();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user