Skip to content

Commit

Permalink
Update DBPDO.php
Browse files Browse the repository at this point in the history
Updated DBPDO to use a a singleton instance if needed throughout the app
  • Loading branch information
a1phanumeric authored Jan 7, 2025
1 parent 9c189d0 commit 7f8c3e3
Showing 1 changed file with 65 additions and 25 deletions.
90 changes: 65 additions & 25 deletions DBPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,124 @@
use \PDO;
use \PDOException;

class DBPDO {
class DBPDO
{

private static $instance = null;
public $pdo;
private $error;
private $dbname;
private $dbhost;
private $dbuser;
private $dbpass;
private $orderwise;

public static function getInstance($dbhost, $dbname, $dbuser, $dbpass, $orderwise = false)
{
if (self::$instance === null) {
self::$instance = new self($dbhost, $dbname, $dbuser, $dbpass, $orderwise);
}
return self::$instance;
}

function __construct($dbhost = '', $dbname = '', $dbuser = '', $dbpass= '') {
function __construct($dbhost = '', $dbname = '', $dbuser = '', $dbpass = '', $orderwise = false)
{
$this->dbhost = $dbhost;
$this->dbname = $dbname;
$this->dbuser = $dbuser;
$this->dbpass = $dbpass;
$this->orderwise = $orderwise;
$this->connect();
}

// Disallow cloning and unserializing
private function __clone() {}
private function __wakeup() {}


function prep_query($query){
function prep_query($query)
{
return $this->pdo->prepare($query);
}


function connect(){
if(!$this->pdo){

$dsn = 'mysql:dbname=' . $this->dbname . ';host=' . $this->dbhost . ';charset=utf8';
function connect()
{
if (!$this->pdo) {
if($this->orderwise){
$dsn = 'sqlsrv:Server=' . $this->dbhost . ';Database=' . $this->dbname . ';Encrypt=no';
}else{
$dsn = 'mysql:dbname=' . $this->dbname . ';host=' . $this->dbhost . ';charset=utf8mb4';
}
$user = $this->dbuser;
$password = $this->dbpass;

try {
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
if($this->orderwise){
$this->pdo = new PDO($dsn, $user, $password);
}else{
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
}
return true;
} catch (PDOException $e) {
$this->error = $e->getMessage();
die($this->error);
// die($this->error);
return false;
}
}else{
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
} else {
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
return true;
}
}


function table_exists($table_name){
function table_exists($table_name)
{
$stmt = $this->prep_query('SHOW TABLES LIKE ?');
$stmt->execute(array($table_name));
return $stmt->rowCount() > 0;
}


function execute($query, $values = null){
if($values == null){
function execute($query, $values = null, $debug = false)
{
if ($values == null) {
$values = array();
}else if(!is_array($values)){
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->prep_query($query);
$stmt->execute($values);
if($debug){
echo $query;
print_r($values);
die();
}
try {
$stmt->execute($values);
} catch (PDOException $e) {
$this->error = $e->getMessage();
die($query . "<br />\n" . $this->error);
return false;
}
return $stmt;
}

function fetch($query, $values = null){
if($values == null){
function fetch($query, $values = null)
{
if ($values == null) {
$values = array();
}else if(!is_array($values)){
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->execute($query, $values);
return $stmt->fetch(PDO::FETCH_ASSOC);
}

function fetchAll($query, $values = null, $key = null){
if($values == null){
function fetchAll($query, $values = null, $key = null)
{
if ($values == null) {
$values = array();
}else if(!is_array($values)){
} else if (!is_array($values)) {
$values = array($values);
}
$stmt = $this->execute($query, $values);
Expand All @@ -91,7 +131,7 @@ function fetchAll($query, $values = null, $key = null){
// Allows the user to retrieve results using a
// column from the results as a key for the array
if(!empty($results)){
if ($key != null && $results[0][$key]) {
if ($key != null) {
$keyed_results = array();
foreach ($results as $result) {
$keyed_results[$result[$key]] = $result;
Expand All @@ -102,8 +142,8 @@ function fetchAll($query, $values = null, $key = null){
return $results;
}

function lastInsertId(){
function lastInsertId()
{
return $this->pdo->lastInsertId();
}

}

0 comments on commit 7f8c3e3

Please sign in to comment.