sealandia-framework/database.php

144 lines
3.8 KiB
PHP

<?php
use PDO;
Class Database{
private $pdo;
private $type;
private $table;
private $columns;
private $where;
private $limit;
private $query;
/**
* Constructor method to initialize the PDO instance
*/
public function __construct(){
$this->pdo = new PDO('mysql:host=' . $db_config['host'] . ';dbname=' . $db_config['database'] . ';charset=utf8mb4', $db_config['username'], $db_config['password']);
}
/**
* Method to start a SELECT query and specify columns
*/
public function select($columns = "*"){
$this->type = "SELECT";
$this->columns = $columns;
}
/**
* Method to specify the table to query
*/
public function from($table){
$this->table = $table;
}
/**
* Method to add a WHERE clause to the query
*/
public function where($key, $value){
$this->where = array($key, $value);
}
/**
* Method to add additional conditions to the WHERE clause
*/
public function andWhere($key, $value){
if(!$this->where) $this->where = array();
$this->where[$key] = $value;
}
/**
* Method to add a LIMIT clause to the query
*/
public function limit($number){
$this->limit = $number;
}
/**
* Method to execute the query and return all results
*/
public function all(){
$this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table;
/**
* Check if a WHERE clause is set, if so, then append it to the query.
* Then construct WHERE conditions using placeholders for prepared statements and
* join the WHERE conditions with AND and append them to the query
*/
if(!empty($this->where)){
$whereClauses = [];
foreach($this->where as $condition){
$whereClauses[] = $condition[0] . ' = :"' . $condition[0];
}
$this->query .= ' WHERE ' . implode(' AND ', $whereClauses);
}
if(!empty($this->limit)){
$this->query .= ' LIMIT ' . $this->limit;
}
$results = $this->pdo->prepare($Query);
/**
* Bind the values from the WHERE clause to the prepared statement
*/
foreach($this->where as $condition){
$results->bindValue(':' . $condition[0], $condition[1]);
}
$results->execute();
return $results->fetchAll();
}
/**
* Method to execute the query and return a single result
*/
public function one(){
$this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table;
/**
* Check if a WHERE clause is set, if so, then append it to the query.
* Then construct WHERE conditions using placeholders for prepared statements and
* join the WHERE conditions with AND and append them to the query
*/
if(!empty($this->where)){
$whereClauses = [];
foreach($this->where as $condition){
$whereClauses[] = $condition[0] . ' = :"' . $condition[0];
}
$this->query .= ' WHERE ' . implode(' AND ', $whereClauses);
}
$this->query .= " LIMIT 1";
$results = $this->pdo->prepare($Query);
/**
* Bind the values from the WHERE clause to the prepared statement
*/
foreach($this->where as $condition){
$results->bindValue(':' . $condition[0], $condition[1]);
}
$results->execute();
return $results->fetch();
}
}
// Database::select(id, name)->from('tablename')->all()
// Database::select(id, name)->from('tablename')->limit(100)->all()
// Database::select()->from('tablename')->where('id', 3)->one()
?>