Last active
April 2, 2016 14:19
-
-
Save WopsS/00600791fee7d3607987acc3832fc3b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Database | |
{ | |
/* @var PDO $DatabaseHandler */ | |
private $DatabaseHandler = null; | |
/* @var PDOStatement $QueryHandler */ | |
private $QueryHandler = null; | |
private $ConnectionString = null, $UserName = null, $Password = null; | |
/** | |
* Class constructor. Connect to MySQL when constructor is called. | |
* | |
* @param string $Host Server IP address or DNS. | |
* @param string $Database Name of database. | |
* @param string $UserName Username for database. | |
* @param string $Password Password for username. | |
* | |
* @throws Exception MySQL connection error. | |
*/ | |
public function __construct($Host, $Database, $UserName, $Password) | |
{ | |
$this->ConnectionString = "mysql:host=" . $Host . ";dbname=" . $Database; | |
$this->UserName = $UserName; | |
$this->Password = $Password; | |
if ($this->DatabaseHandler == null) | |
{ | |
$this->Connect(); | |
} | |
} | |
/** | |
* Open connection to MySQL database. | |
* | |
* @throws Exception MySQL connection error. | |
*/ | |
private function Connect() | |
{ | |
try | |
{ | |
$this->DatabaseHandler = new PDO($this->ConnectionString, $this->UserName, $this->Password, array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); | |
} | |
catch (PDOException $e) | |
{ | |
throw new Exception($e->getMessage()); | |
} | |
} | |
/** | |
* Close connection to MySQL database. | |
*/ | |
private function Disconnect() | |
{ | |
$this->DatabaseHandler = null; | |
} | |
/** | |
* Initiates a transaction. | |
* | |
* @return bool Return TRUE on success or FALSE on failure. | |
*/ | |
public function BeginTransaction() | |
{ | |
return $this->DatabaseHandler->beginTransaction(); | |
} | |
/** | |
* Rolls back a transaction. | |
* | |
* @return bool Return TRUE on success or FALSE on failure. | |
*/ | |
public function CancelTransaction() | |
{ | |
return $this->DatabaseHandler->rollBack(); | |
} | |
/** | |
* Returns the number of rows affected by the last SQL statement. | |
* | |
* @return int Return the number of rows. | |
*/ | |
public function CountRows() | |
{ | |
return $this->QueryHandler->rowCount(); | |
} | |
/** | |
* Dump an SQL prepared command. | |
*/ | |
public function DebugDumpParams() | |
{ | |
return $this->QueryHandler->debugDumpParams(); | |
} | |
/** | |
* Commits a transaction. | |
* | |
* @return bool Return TRUE on success or FALSE on failure. | |
*/ | |
public function EndTransaction() | |
{ | |
return $this->DatabaseHandler->commit(); | |
} | |
/** | |
* Execute a query with binded parameters. | |
* | |
* @param string $Query Query to be executed with parameters. | |
* @param array $Parameters All parameters name from Query without ":". | |
* @param bool $OneRow If this is TRUE then it will return just one row, otherwise return all rows. | |
* | |
* @return array|int|mixed Return 1 if is not a SELECT query, otherwise values from SELECT query as an array. | |
*/ | |
public function Execute($Query, $Parameters = null, $OneRow = false) | |
{ | |
$this->QueryHandler = $this->DatabaseHandler->prepare($Query); | |
// If $Parameters aren't null, it will bind the parameters. | |
if ($Parameters != null) | |
{ | |
foreach ($Parameters as $Name => &$Value) | |
{ | |
switch (true) | |
{ | |
case is_int($Value): | |
$ParameterType = PDO::PARAM_INT; | |
break; | |
case is_bool($Value): | |
$ParameterType = PDO::PARAM_BOOL; | |
break; | |
case is_null($Value): | |
$ParameterType = PDO::PARAM_NULL; | |
break; | |
default: | |
$ParameterType = PDO::PARAM_STR; | |
} | |
$this->QueryHandler->bindParam(":" . $Name, $Value, $ParameterType, ($ParameterType == PDO::PARAM_STR) ? strlen($Value) : 0); | |
} | |
} | |
$this->QueryHandler->execute(); | |
if (strpos($Query, "SELECT") === false) | |
{ | |
return 1; | |
} | |
if ($OneRow === false) | |
{ | |
return $this->QueryHandler->fetchAll(PDO::FETCH_ASSOC); | |
} | |
else | |
{ | |
return $this->QueryHandler->fetch(PDO::FETCH_ASSOC); | |
} | |
} | |
/** | |
* Returns the ID of the last inserted row or sequence value. | |
* | |
* @param string $Name Name of the sequence object from which the ID should be returned. | |
* | |
* @return string Return if a sequence name was not specified for the name parameter, PDO::lastInsertId() returns a string representing the row ID of the last row that was inserted into the database. If a sequence | |
* name was specified for the name parameter, PDO::lastInsertId() returns a string representing the last value retrieved from the specified sequence object. | |
*/ | |
public function LastInsertID($Name = null) | |
{ | |
return $this->DatabaseHandler->lastInsertId($Name); | |
} | |
/** | |
* Class destructor, also it will close MySQL connection. | |
*/ | |
public function __destruct() | |
{ | |
$this->Disconnect(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment