changes
This commit is contained in:
parent
9aca57c01f
commit
f8fe1a835c
62
database.php
62
database.php
|
@ -3,7 +3,6 @@
|
||||||
use PDO;
|
use PDO;
|
||||||
|
|
||||||
Class Database{
|
Class Database{
|
||||||
global $db_config;
|
|
||||||
private $pdo;
|
private $pdo;
|
||||||
private $type;
|
private $type;
|
||||||
private $table;
|
private $table;
|
||||||
|
@ -12,35 +11,69 @@ Class Database{
|
||||||
private $limit;
|
private $limit;
|
||||||
private $query;
|
private $query;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor method to initialize the PDO instance
|
||||||
|
*/
|
||||||
|
|
||||||
public function __construct(){
|
public function __construct(){
|
||||||
$this->pdo = new PDO('mysql:host=' . $db_config['host'] . ';dbname=' . $db_config['database'] . ';charset=utf8mb4', $db_config['username'], $db_config['password']);
|
$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 = "*"){
|
public function select($columns = "*"){
|
||||||
$this->type = "SELECT";
|
$this->type = "SELECT";
|
||||||
$this->columns = $columns;
|
$this->columns = $columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to specify the table to query
|
||||||
|
*/
|
||||||
|
|
||||||
public function from($table){
|
public function from($table){
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function where($key, value){
|
/**
|
||||||
|
* Method to add a WHERE clause to the query
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function where($key, $value){
|
||||||
$this->where = array($key, $value);
|
$this->where = array($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function andWhere($key, value){
|
/**
|
||||||
|
* Method to add additional conditions to the WHERE clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function andWhere($key, $value){
|
||||||
if(!$this->where) $this->where = array();
|
if(!$this->where) $this->where = array();
|
||||||
$this->where[$key] = $value;
|
$this->where[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to add a LIMIT clause to the query
|
||||||
|
*/
|
||||||
|
|
||||||
public function limit($number){
|
public function limit($number){
|
||||||
$this->limit = $number;
|
$this->limit = $number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to execute the query and return all results
|
||||||
|
*/
|
||||||
|
|
||||||
public function all(){
|
public function all(){
|
||||||
$this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table;
|
$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)){
|
if(!empty($this->where)){
|
||||||
$whereClauses = [];
|
$whereClauses = [];
|
||||||
foreach($this->where as $condition){
|
foreach($this->where as $condition){
|
||||||
|
@ -54,6 +87,11 @@ Class Database{
|
||||||
}
|
}
|
||||||
|
|
||||||
$results = $this->pdo->prepare($Query);
|
$results = $this->pdo->prepare($Query);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind the values from the WHERE clause to the prepared statement
|
||||||
|
*/
|
||||||
|
|
||||||
foreach($this->where as $condition){
|
foreach($this->where as $condition){
|
||||||
$results->bindValue(':' . $condition[0], $condition[1]);
|
$results->bindValue(':' . $condition[0], $condition[1]);
|
||||||
}
|
}
|
||||||
|
@ -62,9 +100,19 @@ Class Database{
|
||||||
return $results->fetchAll();
|
return $results->fetchAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to execute the query and return a single result
|
||||||
|
*/
|
||||||
|
|
||||||
public function one(){
|
public function one(){
|
||||||
$this->query = $this->type . ' ' . $this->columns . ' FROM ' . $this->table;
|
$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)){
|
if(!empty($this->where)){
|
||||||
$whereClauses = [];
|
$whereClauses = [];
|
||||||
foreach($this->where as $condition){
|
foreach($this->where as $condition){
|
||||||
|
@ -73,9 +121,13 @@ Class Database{
|
||||||
$this->query .= ' WHERE ' . implode(' AND ', $whereClauses);
|
$this->query .= ' WHERE ' . implode(' AND ', $whereClauses);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->query .= " LIMIT " . $this->limit;
|
$this->query .= " LIMIT 1";
|
||||||
|
|
||||||
$results = $this->pdo->prepare($Query);
|
$results = $this->pdo->prepare($Query);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bind the values from the WHERE clause to the prepared statement
|
||||||
|
*/
|
||||||
|
|
||||||
foreach($this->where as $condition){
|
foreach($this->where as $condition){
|
||||||
$results->bindValue(':' . $condition[0], $condition[1]);
|
$results->bindValue(':' . $condition[0], $condition[1]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,6 +53,6 @@ class Router{
|
||||||
// '/contact' => 'controllers/contact.php',
|
// '/contact' => 'controllers/contact.php',
|
||||||
// ];
|
// ];
|
||||||
|
|
||||||
// $router = (new Router)->routeToController($routes);
|
// $router = Router::routeToController($routes);
|
||||||
|
|
||||||
?>
|
?>
|
|
@ -20,6 +20,6 @@ class Template{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// $templ = (new Template)->render('about', '['title' => 'About']');
|
// $templ = Template::render('about', '['title' => 'About']');
|
||||||
|
|
||||||
?>
|
?>
|
Loading…
Reference in New Issue