Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // _____
- // ___ _ __|___ /_ __
- // / __| '_ \ |_ \ \/ /
- // \__ \ |_) |__) > <
- // |___/ .__/____/_/\_\
- // |_| info@sp3x.de
- //
- // @author sp3x
- // @copyright sp3x / sp3x.de
- // @license All rights reserved
- // @version $Revision 23 $
- // @date $17.09.2009 $
- //
- class Database
- {
- //
- // MySQLi Object
- // @var mysqli
- //
- private $hMySQLi;
- //
- // Debugging mode
- // @var boolean
- //
- private $debug;
- //
- // MySQLi Result object
- // @var MySQLi::Result
- //
- private $hResult;
- //
- // MySQLi Statement object
- // @var MySQLi::STMT
- //
- private $hStmt;
- //
- // Parameters for prepared statements
- // @var array
- //
- private $params;
- //
- // Table prefix
- // @var string
- //
- private $pref;
- //
- // Bound assoc columns
- // @var boolean
- //
- private $bound;
- //
- // Database constructor
- // -----------------------------------------------------
- // @param boolean Debugging
- //
- public function __construct($debug = false)
- {
- $this->debug = $debug;
- }
- //
- // Connect to mysql database
- // -----------------------------------------------------
- // @param string Hostname
- // @param string Username
- // @param string Password
- // @param integer Port
- // @param boolean Persistente connection
- //
- public function connect($host = '127.0.0.1', $user = 'root', $pass = '', $port = 3306,
- $pref = '', $pconn = false)
- {
- $this->pref = $pref;
- if($pconn)
- $host = 'p:'.$host; // Use persistent connection so prefix host with "p:"
- $this->hMySQLi = @new mysqli($host, $user, $pass, '', $port);
- if(!$this->hMySQLi->connect_error)
- return true;
- $this->debug();
- return false;
- }
- //
- // Closes database connection an deletes mysqli object
- // -----------------------------------------------------
- //
- public function disconnect()
- {
- @$this->hMySQLi->close();
- $this->hMySQLi = null;
- unset($this->hMySQLi);
- }
- //
- // Returns error and errno if an error occurs
- // -----------------------------------------------------
- // @param array error[0] and errno[1]
- // @return mixed Error or false on no error
- //
- public function error()
- {
- // Returns errors occurs on establish connection to mysqld
- if($this->hMySQLi->connect_error)
- {
- return array($this->hMySQLi->connect_error, $this->hMySQLi->connect_errno, 'connect');
- }
- // Returns errors occurs on mysql usage
- if($this->hMySQLi->error)
- {
- return array($this->hMySQLi->error, $this->hMySQLi->errno, 'core');
- }
- // Returns errors occurs while executing stmt's
- if(is_object($this->hStmt) && $this->hStmt->error)
- {
- return array($this->hStmt->error, $this->hStmt->errno, 'stmt');
- }
- return false;
- }
- //
- // Selects specified default database for querys
- // -----------------------------------------------------
- // @param string Database name
- // @return boolean Database selection state
- //
- public function dbname($dbName)
- {
- $retVal = @$this->hMySQLi->select_db($dbName);
- $this->debug();
- return $retVal;
- }
- //
- // Executes a query on the mysql server
- // -----------------------------------------------------
- // @param mixed Query-String or null on prepared statement
- // @param mixed Prepared statement parameter
- // @return boolean True on success, false on fail
- //
- public function query($query = null, &$args = null)
- {
- if(($query == null) && is_object($this->hStmt))
- {
- // Build parameter array for bind_result
- if(is_array($args))
- {
- $params[0] = '';
- $count = count($args);
- for($i=0; $i<$count; $i++)
- {
- $params[0] .= $args[$i][0];
- $params[] = &$args[$i][1];
- }
- call_user_func_array(array($this->hStmt, 'bind_param'), $params);
- }
- // Execute prepared statement
- $this->hResult = $this->hStmt->execute();
- } else
- { // Execute simple statement
- $this->resetStmt();
- $this->hResult = $this->hMySQLi->query($query, MYSQLI_USE_RESULT);
- }
- if($this->hStmt)
- return true;
- $this->debug();
- return false;
- }
- //
- // Fetch result as assoc array
- // -----------------------------------------------------
- // @return mixed Assoc-Array or FALSE
- //
- public function fetch()
- {
- if(is_object($this->hStmt))
- {
- // Bind columns as references fpr assoc result
- if($this->bound === false)
- {
- $fields = array();
- $meta = $this->hStmt->result_metadata();
- $columns = $meta->fetch_fields();
- foreach($columns as $column)
- {
- $fields[] = &$this->bound[$column->name];
- }
- call_user_func_array(array($this->hStmt, 'bind_result'), $fields);
- }
- // Fillout assoc result
- if($this->hStmt->fetch())
- return $this->bound;
- } elseif($this->hResult)
- {
- return $this->hResult->fetch_assoc();
- }
- return false;
- }
- //
- // Fetch all rows into one array
- // -----------------------------------------------------
- // @return mixed Assoc-Array or FALSE
- //
- public function fetchAll()
- {
- $result = array();
- while($result[] = $this->fetch())
- if(count($result))
- return $result;
- return false;
- }
- //
- // Prepares query for execution
- // -----------------------------------------------------
- // @param string Query
- // @return boolean Prepare state
- //
- public function prepare($query)
- {
- $this->resetStmt();
- if($this->hStmt = $this->hMySQLi->prepare($query))
- return true;
- $this->debug();
- return false;
- }
- //
- // Starts transaction if database engine
- // supports it (like InnoDB, NOT MyISAM)
- // -----------------------------------------------------
- //
- public function transaction()
- {
- $this->resetStmt();
- // Disable autocommit so transactions are not commited automatically (Transaction begin)
- $this->hMySQLi->autocommit(false);
- $this->debug();
- }
- //
- // Rollbacks active transaction
- // -----------------------------------------------------
- //
- public function rollback()
- {
- $this->hMySQLi->rollback();
- // Re-Enable autocommit (Transaction end)
- $this->hMySQLi->autocommit(true);
- $this->debug();
- }
- //
- // Commits active transaction
- // -----------------------------------------------------
- //
- public function commit()
- {
- $this->hMySQLi->commit();
- // Re-Enable autocommit (Transaction end)
- $this->hMySQLi->autocommit(true);
- $this->debug();
- }
- //
- // Frees result and stmt objects used memory
- // -----------------------------------------------------
- //
- private function resetStmt($null = true)
- {
- if(is_object($this->hStmt))
- {
- // Reset statement-obj if initialized
- @$this->hStmt->free_result();
- @$this->hStmt->close();
- if($null)
- $this->hStmt = null;
- } elseif(is_object($this->hResult))
- {
- // Reset result-obj
- @$this->hResult->free();
- @$this->hResult->close();
- if($null)
- $this->hStmt = null;
- }
- $this->bound = false;
- }
- //
- // Debugging function displays error messages if an error
- // occurs
- // -----------------------------------------------------
- //
- private function debug()
- {
- if($this->debug)
- {
- $error = $this->error();
- if($error)
- {
- echo "MySQLi - Debugging Mode ({$error[2]}):\n----------------------------------\n";
- echo "Error: {$error[0]}\nErrno: {$error[1]}\n\n";
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement