This commit is contained in:
Jordy van Zeeland 2024-08-15 08:54:44 +02:00
parent 9aca57c01f
commit f8fe1a835c
3 changed files with 59 additions and 7 deletions

View File

@ -3,7 +3,6 @@
use PDO;
Class Database{
global $db_config;
private $pdo;
private $type;
private $table;
@ -12,35 +11,69 @@ Class Database{
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;
}
public function where($key, value){
/**
* Method to add a WHERE clause to the query
*/
public function where($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();
$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){
@ -54,6 +87,11 @@ Class Database{
}
$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]);
}
@ -62,9 +100,19 @@ Class Database{
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){
@ -73,9 +121,13 @@ Class Database{
$this->query .= ' WHERE ' . implode(' AND ', $whereClauses);
}
$this->query .= " LIMIT " . $this->limit;
$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]);
}

View File

@ -53,6 +53,6 @@ class Router{
// '/contact' => 'controllers/contact.php',
// ];
// $router = (new Router)->routeToController($routes);
// $router = Router::routeToController($routes);
?>

View File

@ -20,6 +20,6 @@ class Template{
}
// $templ = (new Template)->render('about', '['title' => 'About']');
// $templ = Template::render('about', '['title' => 'About']');
?>