2024-04-15 11:38:59 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
|
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Description of Database
|
|
|
|
*
|
|
|
|
* @author SCTN4
|
|
|
|
*/
|
|
|
|
class Database {
|
|
|
|
protected $connection = null;
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
try {
|
2024-05-02 02:19:13 -05:00
|
|
|
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
2024-04-15 11:38:59 -05:00
|
|
|
$this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME);
|
|
|
|
|
|
|
|
if ( mysqli_connect_errno()) {
|
|
|
|
throw new Exception("Could not connect to database.");
|
|
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new Exception($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processQuery($query = "", $params = [])
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$stmt = $this->executeQuery( $query, $params );
|
|
|
|
$result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
|
|
|
|
$stmt->close();
|
|
|
|
return $result;
|
|
|
|
} catch(Exception $e) {
|
|
|
|
throw New Exception( $e->getMessage() );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function executeQuery($query = "" , $params = [])
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$stmt = $this->connection->prepare( $query );
|
|
|
|
if($stmt === false) {
|
|
|
|
throw New Exception("Unable to do prepared statement: " . $query);
|
|
|
|
}
|
|
|
|
if( $params ) {
|
|
|
|
$stmt->bind_param($params[0], $params[1]);
|
|
|
|
}
|
|
|
|
$stmt->execute();
|
|
|
|
return $stmt;
|
|
|
|
} catch(Exception $e) {
|
|
|
|
throw New Exception( $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processStatement($query = "", $params = [])
|
|
|
|
{
|
|
|
|
try {
|
2024-05-02 02:19:13 -05:00
|
|
|
$parameters = str_repeat('?,', count($params) - 1) . '?';
|
|
|
|
$query += "(".$parameters.")";
|
2024-04-15 11:38:59 -05:00
|
|
|
$stmt = $this->executeStatement( $query, $params );
|
|
|
|
$result = $this->connection->affected_rows;
|
|
|
|
$stmt->close();
|
|
|
|
return $result;
|
|
|
|
} catch(Exception $e) {
|
|
|
|
throw New Exception( $e->getMessage() );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function executeStatement($query = "", $params = [])
|
|
|
|
{
|
|
|
|
try {
|
2024-05-02 02:19:13 -05:00
|
|
|
$stmt = $this->connection->prepare($query);
|
2024-04-15 11:38:59 -05:00
|
|
|
if($stmt === false) {
|
|
|
|
throw New Exception("Unable to do prepared statement: " . $query);
|
|
|
|
}
|
2024-05-02 02:19:13 -05:00
|
|
|
|
|
|
|
/*if( $params ) {
|
2024-05-01 15:03:43 -05:00
|
|
|
$stmt->bind_param(str_repeat("s", count($params)), ...$params);
|
2024-05-02 02:19:13 -05:00
|
|
|
}*/
|
|
|
|
|
|
|
|
$stmt->execute($params);
|
|
|
|
//$stmt->execute();
|
2024-04-15 11:38:59 -05:00
|
|
|
return $stmt;
|
|
|
|
} catch(Exception $e) {
|
|
|
|
throw New Exception( $e->getMessage() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|