I made the following of the mysql class:
<?php
/**
* Database class
*
* Establishes a database connection
*
* @author Huang_hanzen <
[email protected]>
* @edited Jonathan Dequeker <
[email protected]>
*/
class database {
/**
* @var string Name of database host
*/
private $host = "localhost";
/**
* @var string
*/
private $username;
/**
* @var string
*/
private $password;
/**
* @var string Database name
*/
private $database;
/**
* @var object Must be instance of PDO
*/
private $connection;
/**
* @var array List of occured errors
*/
public $errors = array();
/**
* set data if given, connect if possible
* @param string $host
* @param string $username
* @param string $password
* @param string $database
* @param boolean $warn Set to true to throw error on non-connection
* @return bool
*/
public function __construct($host, $username, $password, $database, $warn = false)
{
$this->setData($host, $username, $password, $database);
try{
$connected = $this->connect();
}catch(Exception $e) {
return false;
}
return $connected;
}
/**
* connect to MySQL database
* @param string $host
* @param string $username
* @param string $password
* @param string $database
* @return bool
*/
public function connect($host = NULL, $username = NULL, $password = NULL, $database = NULL)
{
$this->setData($host, $username, $password, $database);
try{
if(!isSet($this->host) or !is_string($this->host)) { throw new Exception("Host name not set."); }
if(!isSet($this->username) or !is_string($this->username)) { throw new Exception("Username not set."); }
if(!isSet($this->password) or !is_string($this->password)) { throw new Exception("Password not set."); }
if(!isSet($this->database) or !is_string($this->database)) { throw new Exception("Database name not set."); }
$this->connection = new PDO('mysql:host=localhost;dbname='.$this->database, $this->username, $this->password);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}catch(PDOException $e) {
$this->error($e->getMessage());
return false;
}catch(Exception $e) {
$this->error($e->getMessage());
return false;
}
return true;
}
/**
* set database configurations
* @param string $host
* @param string $username
* @param string $password
* @param string $database
*/
public function setData($host = NULL, $username = NULL, $password = NULL, $database = NULL)
{
if(isSet($host) and is_string($host)) { $this->host = $host; }
if(isSet($username) and is_string($username)) { $this->username = $username; }
if(isSet($password) and is_string($password)) { $this->password = $password; }
if(isSet($database) and is_string($database)) { $this->database = $database; }
}
/**
* throws a databaseError or logs error
* @param string $message
* @param integer $code
* @return string If the error was logged, the error key
*/
public function error($message = NULL, $code = NULL)
{
if($this->exceptions) {
throw new databaseError($message,$code);
return;
}else{
$this->errors[date("H:i:s",time())] = array($code => $message);
return date("H:i:s",time());
}
}
/**
* get last error
* @param string $message
* @param integer $code
* @return string
*/
public function getError()
{
if($last = end((array) $this->errors)) {
return $last[0];
}else{
return false;
}
}
/**
* unset the connection
*/
public function __destruct()
{
$this->connection = NULL;
}
}
?>