Advertisement
Guest User

Untitled

a guest
May 31st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.26 KB | None | 0 0
  1. <?php
  2.  
  3. if(phpversion() < 5)
  4.     die('The class DB needs at least PHP5');
  5.    
  6. class DB{
  7.     var $ressource;
  8.     var $statement;
  9.     var $hide_errors = FALSE;
  10.     var $direct_error_output = TRUE;
  11.     var $connected = FALSE;
  12.     var $statement_set = FALSE;
  13.    
  14.     var $app = 'App';
  15.    
  16.     var $host = '';
  17.     var $username = '';
  18.     var $password = '';
  19.     var $database = '';
  20.    
  21.     function __construct($host, $username, &$password, $database){
  22.         $this->host = $host;
  23.         $this->username = $username;
  24.         $this->password = $password;
  25.         $this->database = $database;
  26.        
  27.         $password = '';
  28.        
  29.         $this->connect();
  30.        
  31.         $this->password = '';
  32.     }
  33.    
  34.     function connect(){
  35.         //Ignore connection error so we can let the class handle it
  36.         @$this->ressource = new MySQLi($this->host, $this->username, $this->password);
  37.         if(!$this->check_for_sql_errors(TRUE)){
  38.             $this->produce_error('Connection to database could not be established.');
  39.         }
  40.        
  41.         $this->connected = TRUE;
  42.        
  43.         if(!empty($this->database))
  44.             $this->select_db();
  45.     }
  46.    
  47.     function select_db(){
  48.         if(!empty($this->database)){
  49.             $this->ressource->select_db($this->database);
  50.         }
  51.         if(!$this->check_for_sql_errors()){
  52.             $this->produce_error('Database could not be choosen.');
  53.         }
  54.     }
  55.    
  56.     function set_charset($charset = NULL){
  57.         if($this->connected === TRUE){
  58.             if(!$this->ressource->set_charset($charset)){
  59.                 $this->produce_error('Default charset could not be set.');
  60.             }
  61.         }  
  62.     }
  63.    
  64.     function get_sql_version(){
  65.         if($this->connected === TRUE){
  66.             $sql = "SELECT VERSION() AS version";
  67.             $statement = new StmtMySQLi($this->ressource);
  68.             $statement->prepare($sql);
  69.             $statement->execute();
  70.             $version = $statement->getOne();
  71.             $version = $version['version'];
  72.         }
  73.         if(empty($version)) $version = 'unknown';
  74.         return $version;
  75.     }
  76.    
  77.     function query(){
  78.         $args = func_get_args();
  79.        
  80.         if($this->connected !== TRUE){
  81.             $this->produce_error('No database connection');
  82.         }
  83.  
  84.         $this->statement = new StmtMySQLi($this->ressource);
  85.         $this->statement_set = FALSE;
  86.        
  87.         if(!empty($args[0])){
  88.             $sql = array_shift($args);
  89.             $types = '';
  90.             foreach ($args AS &$value){
  91.                 switch ($value){
  92.                     case (is_numeric($value) && filter_var($value, FILTER_SANITIZE_NUMBER_INT)):
  93.                         $types .= 'i';
  94.                         debug(is_int($value), 0);
  95.                         var_dump($value);
  96.                     break;
  97.                    
  98.                     case (is_numeric($value) && filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION | FILTER_FLAG_ALLOW_THOUSAND)):
  99.                         $types .= 'd';
  100.                     break;
  101.  
  102.                     case (filter_var($value, FILTER_SANITIZE_STRING)):
  103.                         $types .= 's';
  104.                     break;
  105.                 }
  106.             }
  107.             debug($args, 0);
  108.             debug($types);
  109.             if(!$this->statement->prepare($sql)){
  110.                 $this->produce_error('Query is invalid');
  111.             }
  112.             else{
  113.                 $this->statement_set = TRUE;
  114.                 if(isset($args[0])){
  115.                     array_unshift($args, $types);
  116.                     if(!empty($args) && count($args) > 0){
  117.                         if(!call_user_func_array(array($this->statement, 'bind_param'), $args)){
  118.                             $this->produce_error('Parameters are invalid');
  119.                         }
  120.                     }
  121.                 }
  122.                 $this->statement->execute();
  123.                 $this->statement->store_result();
  124.                 $this->get_query_info();
  125.                 return $this->statement;
  126.             }
  127.         }
  128.         else{
  129.             $this->produce_error('Query must not be empty');
  130.         }  
  131.     }
  132.        
  133.     function fetch_first(){
  134.         $args = func_get_args();
  135.        
  136.         $statement = call_user_func_array(array($this, 'query'), $args);
  137.         if($statement instanceof StmtMySQLi){
  138.             $result = $statement->getOne();
  139.         }
  140.         else{
  141.             $result = FALSE;
  142.         }
  143.        
  144.         return $result;
  145.     }
  146.    
  147.     function fetch_all($statement = -1, $type = MYSQLI_BOTH){
  148.         if($statement == -1) $statement = &$this->statement;
  149.         if($this->check_for_sql_errors() && $statement){
  150.             return $statement->getAll($type);
  151.         }
  152.         else{
  153.             $this->produce_error('No result to fetch from');
  154.         }
  155.     }
  156.    
  157.     function fetch_array($statement = -1, $type = MYSQLI_BOTH){
  158.         if($statement === -1) $statement = &$this->statement;
  159.         if($this->check_for_sql_errors() && $statement){
  160.             return $statement->getOne($type);
  161.         }
  162.         else{
  163.             $this->produce_error('No result to fetch from');
  164.         }
  165.     }
  166.    
  167.     function get_query_info(){
  168.         if(($err = $this->check_for_sql_errors()) && $this->statement_set === TRUE){
  169.             if ($this->statement->affected_rows > 0)
  170.                 $num_rows = $this->statement->affected_rows;
  171.             else
  172.                 $num_rows = $this->statement->num_rows;
  173.                
  174.             $result['num_rows'] = $num_rows;
  175.             $result['insert_id'] = $this->statement->insert_id;
  176.             $result['err_occ'] = !$err;
  177.         }
  178.         elseif($this->statement_set === FALSE){
  179.             $result['err_occ'] = $err;
  180.             $result['num_rows'] = 0;
  181.         }
  182.        
  183.         $this->num_rows = $result['num_rows'];
  184.  
  185.         return $result;
  186.     }
  187.    
  188.     //Error-Sektion
  189.     function produce_error($error_msg){
  190.    
  191.         $error = $this->get_sql_error();
  192.  
  193.         $output = "<b>$this->app SQL Error</b> \n<br />";
  194.         $output .= "<b>An error occurred:</b> $error_msg\n <br />";
  195.         $output .= "<b>mysql error:</b> ".$error['error']."\n <br/>";
  196.         $output .= "<b>mysql error number:</b> ".$error['errorno']."\n <br />";
  197.         $output .= "<b>mysql version:</b> ".$this->get_sql_version()."\n <br />";
  198.         $output .= "<b>php version:</b> ".phpversion()."\n <br />";
  199.         $output .= "<b>Script:</b> ".htmlentities(getenv("REQUEST_URI"))."\n<br>";
  200.  
  201.         if($this->hide_errors == TRUE)
  202.             $output = "<!-- $output -->";
  203.  
  204.         if($this->direct_error_output == TRUE){
  205.             die('<h2>SQL DATABASE ERROR</h2> '.$output);
  206.         }
  207.         else{
  208.             return $output;
  209.         }
  210.     }
  211.    
  212.     function get_sql_error($connect = FALSE){
  213.         $result = array();
  214.         if($connect === TRUE || ($connect == FALSE && mysqli_connect_error())){
  215.             $result['error'] = mysqli_connect_error();
  216.             $result['errorno'] = mysqli_connect_errno();
  217.         }
  218.         else{
  219.             if(!$this->ressource->error){
  220.                 $result['error'] = mysqli_error($this->ressource);
  221.                 $result['errorno'] = mysqli_errno($this->ressource);
  222.             }
  223.             else{
  224.                 $result['error'] = $this->ressource->error;
  225.                 $result['errorno'] = $this->ressource->errno;
  226.             }
  227.         }
  228.         return $result;
  229.     }
  230.    
  231.     //no error occured = TRUE; error occured = FALSE
  232.     function check_for_sql_errors($connect = FALSE){
  233.         $error = $this->get_sql_error($connect);
  234.         if($error['errorno'] === 0)
  235.             return TRUE;
  236.         else
  237.             return FALSE;
  238.     }
  239. }
  240.  
  241.  
  242. class StmtMySQLi extends mysqli_stmt
  243. {
  244.  
  245.     /**
  246.     * Fetch one result (array)
  247.     *
  248.     * @param MYSQLI_RESULTMODE_TYPE $resultmode
  249.     * @return array $result
  250.     */
  251.     public function getOne($resultmode = MYSQLI_BOTH)
  252.     {
  253.         $result = $result_both = $row = array();
  254.         $meta = $this->result_metadata();
  255.         while ($field = $meta->fetch_field()) {
  256.             if(in_array($field->name, array_keys($result), TRUE)){
  257.                 $field->name = $field->table.'.'.$field->name;
  258.             }
  259.             switch ($resultmode) {
  260.                 case MYSQLI_ASSOC:
  261.                     $result[$field->name] = &$row[$field->name];
  262.                 break;
  263.  
  264.                 case MYSQLI_NUM:
  265.                     $result[] = &$row[$field->name];
  266.                 break;
  267.  
  268.                 case MYSQLI_BOTH:
  269.                     $result[$field->name] = &$row[$field->name];
  270.                     $result_both[] = &$row[$field->name];
  271.                 break;
  272.             }
  273.         }
  274.  
  275.         call_user_func_array(array($this,'bind_result'), $result);
  276.         if($this->fetch()){
  277.             return $result+$result_both;
  278.         }
  279.         else{
  280.             return FALSE;
  281.         }
  282.     }
  283.    
  284.     public function getAll($resultmode = MYSQLI_BOTH){
  285.         $result = array();
  286.         while($fetch = $this->getOne($resultmode)){
  287.             $result[] = $fetch;
  288.         }
  289.         return $result;
  290.     }
  291. }
  292.  
  293. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement