Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(phpversion() < 5)
- die('The class DB needs at least PHP5');
- class DB{
- var $ressource;
- var $statement;
- var $hide_errors = FALSE;
- var $direct_error_output = TRUE;
- var $connected = FALSE;
- var $statement_set = FALSE;
- var $app = 'App';
- var $host = '';
- var $username = '';
- var $password = '';
- var $database = '';
- function __construct($host, $username, &$password, $database){
- $this->host = $host;
- $this->username = $username;
- $this->password = $password;
- $this->database = $database;
- $password = '';
- $this->connect();
- $this->password = '';
- }
- function connect(){
- //Ignore connection error so we can let the class handle it
- @$this->ressource = new MySQLi($this->host, $this->username, $this->password);
- if(!$this->check_for_sql_errors(TRUE)){
- $this->produce_error('Connection to database could not be established.');
- }
- $this->connected = TRUE;
- if(!empty($this->database))
- $this->select_db();
- }
- function select_db(){
- if(!empty($this->database)){
- $this->ressource->select_db($this->database);
- }
- if(!$this->check_for_sql_errors()){
- $this->produce_error('Database could not be choosen.');
- }
- }
- function set_charset($charset = NULL){
- if($this->connected === TRUE){
- if(!$this->ressource->set_charset($charset)){
- $this->produce_error('Default charset could not be set.');
- }
- }
- }
- function get_sql_version(){
- if($this->connected === TRUE){
- $sql = "SELECT VERSION() AS version";
- $statement = new StmtMySQLi($this->ressource);
- $statement->prepare($sql);
- $statement->execute();
- $version = $statement->getOne();
- $version = $version['version'];
- }
- if(empty($version)) $version = 'unknown';
- return $version;
- }
- function query(){
- $args = func_get_args();
- if($this->connected !== TRUE){
- $this->produce_error('No database connection');
- }
- $this->statement = new StmtMySQLi($this->ressource);
- $this->statement_set = FALSE;
- if(!empty($args[0])){
- $sql = array_shift($args);
- $types = '';
- foreach ($args AS &$value){
- switch ($value){
- case (is_numeric($value) && filter_var($value, FILTER_SANITIZE_NUMBER_INT)):
- $types .= 'i';
- debug(is_int($value), 0);
- var_dump($value);
- break;
- case (is_numeric($value) && filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND)):
- $types .= 'd';
- break;
- case (filter_var($value, FILTER_SANITIZE_STRING)):
- $types .= 's';
- break;
- }
- }
- debug($args, 0);
- debug($types);
- if(!$this->statement->prepare($sql)){
- $this->produce_error('Query is invalid');
- }
- else{
- $this->statement_set = TRUE;
- if(isset($args[0])){
- array_unshift($args, $types);
- if(!empty($args) && count($args) > 0){
- if(!call_user_func_array(array($this->statement, 'bind_param'), $args)){
- $this->produce_error('Parameters are invalid');
- }
- }
- }
- $this->statement->execute();
- $this->statement->store_result();
- $this->get_query_info();
- return $this->statement;
- }
- }
- else{
- $this->produce_error('Query must not be empty');
- }
- }
- function fetch_first(){
- $args = func_get_args();
- $statement = call_user_func_array(array($this, 'query'), $args);
- if($statement instanceof StmtMySQLi){
- $result = $statement->getOne();
- }
- else{
- $result = FALSE;
- }
- return $result;
- }
- function fetch_all($statement = -1, $type = MYSQLI_BOTH){
- if($statement == -1) $statement = &$this->statement;
- if($this->check_for_sql_errors() && $statement){
- return $statement->getAll($type);
- }
- else{
- $this->produce_error('No result to fetch from');
- }
- }
- function fetch_array($statement = -1, $type = MYSQLI_BOTH){
- if($statement === -1) $statement = &$this->statement;
- if($this->check_for_sql_errors() && $statement){
- return $statement->getOne($type);
- }
- else{
- $this->produce_error('No result to fetch from');
- }
- }
- function get_query_info(){
- if(($err = $this->check_for_sql_errors()) && $this->statement_set === TRUE){
- if ($this->statement->affected_rows > 0)
- $num_rows = $this->statement->affected_rows;
- else
- $num_rows = $this->statement->num_rows;
- $result['num_rows'] = $num_rows;
- $result['insert_id'] = $this->statement->insert_id;
- $result['err_occ'] = !$err;
- }
- elseif($this->statement_set === FALSE){
- $result['err_occ'] = $err;
- $result['num_rows'] = 0;
- }
- $this->num_rows = $result['num_rows'];
- return $result;
- }
- //Error-Sektion
- function produce_error($error_msg){
- $error = $this->get_sql_error();
- $output = "<b>$this->app SQL Error</b> \n<br />";
- $output .= "<b>An error occurred:</b> $error_msg\n <br />";
- $output .= "<b>mysql error:</b> ".$error['error']."\n <br/>";
- $output .= "<b>mysql error number:</b> ".$error['errorno']."\n <br />";
- $output .= "<b>mysql version:</b> ".$this->get_sql_version()."\n <br />";
- $output .= "<b>php version:</b> ".phpversion()."\n <br />";
- $output .= "<b>Script:</b> ".htmlentities(getenv("REQUEST_URI"))."\n<br>";
- if($this->hide_errors == TRUE)
- $output = "<!-- $output -->";
- if($this->direct_error_output == TRUE){
- die('<h2>SQL DATABASE ERROR</h2> '.$output);
- }
- else{
- return $output;
- }
- }
- function get_sql_error($connect = FALSE){
- $result = array();
- if($connect === TRUE || ($connect == FALSE && mysqli_connect_error())){
- $result['error'] = mysqli_connect_error();
- $result['errorno'] = mysqli_connect_errno();
- }
- else{
- if(!$this->ressource->error){
- $result['error'] = mysqli_error($this->ressource);
- $result['errorno'] = mysqli_errno($this->ressource);
- }
- else{
- $result['error'] = $this->ressource->error;
- $result['errorno'] = $this->ressource->errno;
- }
- }
- return $result;
- }
- //no error occured = TRUE; error occured = FALSE
- function check_for_sql_errors($connect = FALSE){
- $error = $this->get_sql_error($connect);
- if($error['errorno'] === 0)
- return TRUE;
- else
- return FALSE;
- }
- }
- class StmtMySQLi extends mysqli_stmt
- {
- /**
- * Fetch one result (array)
- *
- * @param MYSQLI_RESULTMODE_TYPE $resultmode
- * @return array $result
- */
- public function getOne($resultmode = MYSQLI_BOTH)
- {
- $result = $result_both = $row = array();
- $meta = $this->result_metadata();
- while ($field = $meta->fetch_field()) {
- if(in_array($field->name, array_keys($result), TRUE)){
- $field->name = $field->table.'.'.$field->name;
- }
- switch ($resultmode) {
- case MYSQLI_ASSOC:
- $result[$field->name] = &$row[$field->name];
- break;
- case MYSQLI_NUM:
- $result[] = &$row[$field->name];
- break;
- case MYSQLI_BOTH:
- $result[$field->name] = &$row[$field->name];
- $result_both[] = &$row[$field->name];
- break;
- }
- }
- call_user_func_array(array($this,'bind_result'), $result);
- if($this->fetch()){
- return $result+$result_both;
- }
- else{
- return FALSE;
- }
- }
- public function getAll($resultmode = MYSQLI_BOTH){
- $result = array();
- while($fetch = $this->getOne($resultmode)){
- $result[] = $fetch;
- }
- return $result;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement