<?php
class database{
// @object, The PDO object
private $pdo;
private $db_connected, $error;
private $stmt;
public function __construct($host, $dbname, $user, $pass, $char_set="utf8"){
$dsn = 'mysql:dbname='.$dbname.';host='.$host.'';
try{
$this->pdo = new PDO($dsn, $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES ".$char_set));
// We can now log any exceptions on Fatal error.
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Disable emulation of prepared statements, use REAL prepared statements instead.
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->db_connected = true;
}catch (PDOException $e){
die("Error! Could not connect to the database.<br />".$e->getMessage());
}
}
public function __destruct(){
$this->pdo = null;
$this->db_connected = false;
}
public function query($query){
try{
$this->stmt = $this->pdo->prepare($query);
}catch(PDOException $e){
if( _safemode_ ){ die("Error! Invalid Query.<br />".$query."<br />".$e->getMessage()); }
else{ die("A fatal error occurred."); }
}
}
public function bind($param, $value, $type = null){
if ( is_null($type) ){
switch(true){
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
try{
$this->stmt->execute();
return true;
}catch(PDOException $e){
$this->error = $e->getMessage();
return false;
}
}
public function run(){
$this->execute();
}
public function results(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function num_rows(){
return $this->stmt->rowCount();
}
public function last_insert_id(){
return $this->pdo->lastInsertId();
}
public function begin_transaction(){
return $this->pdo->beginTransaction();
}
public function end_transaction(){
return $this->pdo->commit();
}
public function cancel_transaction(){
return $this->pdo->rollBack();
}
public function dump(){
return $this->stmt->debugDumpParams();
}
}
class MainClass{
protected $database;
function __construct(){
$this->database = new database(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);
}
}
class TheClass extends MainClass{
function SomeFunction(){
$this->database->query("some query text...");
}
}
$foo = new TheClass();
$foo->SomeFunction();
Fatal error: Uncaught Error: Undefined constant "DB_HOST" in /in/MaXe6:127
Stack trace:
#0 /in/MaXe6(141): MainClass->__construct()
#1 {main}
thrown in /in/MaXe6 on line 127
Process exited with code 255.
Warning: Use of undefined constant DB_HOST - assumed 'DB_HOST' (this will throw an Error in a future version of PHP) in /in/MaXe6 on line 127
Warning: Use of undefined constant DB_NAME - assumed 'DB_NAME' (this will throw an Error in a future version of PHP) in /in/MaXe6 on line 127
Warning: Use of undefined constant DB_USER - assumed 'DB_USER' (this will throw an Error in a future version of PHP) in /in/MaXe6 on line 127
Warning: Use of undefined constant DB_PASSWORD - assumed 'DB_PASSWORD' (this will throw an Error in a future version of PHP) in /in/MaXe6 on line 127
Fatal error: Uncaught Error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' in /in/MaXe6:18
Stack trace:
#0 /in/MaXe6(127): database->__construct('DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASSWORD')
#1 /in/MaXe6(141): MainClass->__construct()
#2 {main}
thrown in /in/MaXe6 on line 18
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33
Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in /in/MaXe6 on line 127
Notice: Use of undefined constant DB_NAME - assumed 'DB_NAME' in /in/MaXe6 on line 127
Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in /in/MaXe6 on line 127
Notice: Use of undefined constant DB_PASSWORD - assumed 'DB_PASSWORD' in /in/MaXe6 on line 127
Fatal error: Uncaught Error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' in /in/MaXe6:18
Stack trace:
#0 /in/MaXe6(127): database->__construct('DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASSWORD')
#1 /in/MaXe6(141): MainClass->__construct()
#2 {main}
thrown in /in/MaXe6 on line 18
Process exited with code 255.
Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in /in/MaXe6 on line 127
Notice: Use of undefined constant DB_NAME - assumed 'DB_NAME' in /in/MaXe6 on line 127
Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in /in/MaXe6 on line 127
Notice: Use of undefined constant DB_PASSWORD - assumed 'DB_PASSWORD' in /in/MaXe6 on line 127
Fatal error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' in /in/MaXe6 on line 18
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in /in/MaXe6 on line 127
Notice: Use of undefined constant DB_NAME - assumed 'DB_NAME' in /in/MaXe6 on line 127
Notice: Use of undefined constant DB_USER - assumed 'DB_USER' in /in/MaXe6 on line 127
Notice: Use of undefined constant DB_PASSWORD - assumed 'DB_PASSWORD' in /in/MaXe6 on line 127
Fatal error: Class 'PDO' not found in /in/MaXe6 on line 18
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/MaXe6 on line 6
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/MaXe6 on line 6
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/MaXe6 on line 6
Process exited with code 255.