Initial commit

This commit is contained in:
Porawit Dongwang
2026-09-16 23:20:08 +07:00
commit 0041668dbb
32577 changed files with 3687927 additions and 0 deletions
@@ -0,0 +1,89 @@
<?php
namespace app\Core;
use PDO;
use PDOException;
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $stmt;
private $error;
public function __construct() {
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . ';charset=utf8mb4';
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
);
// Create PDO instance
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch(PDOException $e) {
$this->error = $e->getMessage();
// Fallback for when DB isn't created yet during dev
// echo $this->error;
}
}
// Prepare statement
public function query($sql) {
if ($this->dbh) {
$this->stmt = $this->dbh->prepare($sql);
}
}
// Bind values
public function bind($param, $value, $type = null) {
if(is_null($type)) {
switch(true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
// Execute the prepared statement
public function execute() {
return $this->stmt->execute();
}
// Get multiple records as array of objects
public function resultSet() {
$this->execute();
return $this->stmt->fetchAll();
}
// Get single record as object
public function single() {
$this->execute();
return $this->stmt->fetch();
}
// Get row count
public function rowCount() {
return $this->stmt->rowCount();
}
// Get last insert ID
public function lastInsertId() {
return $this->dbh->lastInsertId();
}
}