Skip to content

Instantly share code, notes, and snippets.

@gelanivishal
Last active June 13, 2017 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gelanivishal/e60e93a893e4adcb14e9164327cb611e to your computer and use it in GitHub Desktop.
Save gelanivishal/e60e93a893e4adcb14e9164327cb611e to your computer and use it in GitHub Desktop.
A mysql database wrapper
<?php
// Database
define('DB_HOST', 'localhost');
define('DB_DB', 'db_name');
define('DB_USER', 'db_user');
define('DB_PASS', 'db_password');
define('DB_ERROR', '/path/to/db_error.log');
/* Class: Database
* Description: manage all database functions and resources
*/
class DatabaseManager {
protected $link;
protected $errors;
protected $persistent = false;
/* Function: __construct($host,$db,$user,$pass)
* Description: create a connection to a Mysql database
* Last Update: Sep 17, 2008
*/
public function __construct($host,$db,$user,$pass) {
$this->errors = Array();
$this->link = false;
$this->connect($host,$user,$pass);
$this->selectDB($db);
// Database is UTF8, we need to set the connection to UTF8 too!
mysqli_set_charset($this->link, 'utf8');
}
/* Function: connect($host, $user, $pass)
* Description: create a Mysql connection
* Last Update: Sep 17, 2008
*/
public function connect($host,$user,$pass) {
$this->link = mysqli_connect($host, $user, $pass) or $this->addError("Connection error", mysqli_error($this->link));
return $this->link;
}
/* Function: selectDB($db)
* Description: select the database of the current connection
* Last Update: Sep 17, 2008
*/
public function selectDB($db) {
return mysqli_select_db($this->link, $db) or $this->addError("Database selection error", mysqli_error($this->link));
}
public function quote($string){
return mysqli_real_escape_string($this->link, $string);
}
/* Function: query($query)
* Description: returns mysql result of a query
* Last Update: Sep 17, 2008
*/
public function query($query) {
$result = mysqli_query($this->link, $query) or $this->addError($query, mysqli_error($this->link));
if($result) return $result;
else return false;
}
/* Function: fetchRow($query)
* Description: return the first result of a query in array format
* Last Update: Sep 17, 2008
*/
public function fetchRow($query) {
$result = $this->query($query);
if(!$result) return false;
$row = mysqli_fetch_array($result, MYSQL_ASSOC);
if($row) return $row;
else return Array();
}
/* Function: fetchAll($query)
* Description: return every results of a query in array format
* Last Update: Sep 17, 2008
*/
public function fetchAll($query)
{
$result = $this->query($query);
if(!$result or count($result)==0) return false;
$aux = Array();
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{$aux[] = $row;}
return $aux;
}
public function numRows($query){
$result = $this->query($query);
return mysqli_num_rows($result);
}
/* Function: getLastId()
* Description: returns the last inserted id
* Last Update: Sep 17, 2008
*/
public function getLastId() {
return mysqli_insert_id($this->link);
}
/* Function: addError($query,$error)
* Description: add an error to the error array
* Last Update: Sep 17, 2008
*/
protected function addError($query, $error) {
$errorItem = Array();
$errorItem['query'] = str_replace("\t","",str_replace("\r\n"," ",$query));
$errorItem['error'] = str_replace("\t","",str_replace("\r\n"," ",$error));
array_push($this->errors,$errorItem);
}
/* Function: __destruct
* Description: save all errors in the error log if needed
* Last Update: Sep 17, 2008
*/
public function __destruct() {
if($this->errors) {
$errorLog = fopen(DB_ERROR,'a+');
foreach($this->errors as $errorItem) {
echo $errorItem['error'];
fwrite($errorLog,"\r\n[".date('Y-m-d H:i:s')."] \r\n ERROR: ".$errorItem['error']." \r\n QUERY: ".$errorItem['query']);
fwrite($errorLog,"\r\n---------------------------------------------------------------");
}
fclose($errorLog);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment