Advertisement
Guest User

Untitled

a guest
May 7th, 2017
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.49 KB | None | 0 0
  1. <?php
  2.  
  3. //           _____    
  4. //  ___ _ __|___ /_  __
  5. // / __| '_ \ |_ \ \/ /
  6. // \__ \ |_) |__) >  <
  7. // |___/ .__/____/_/\_\
  8. //     |_| info@sp3x.de
  9. //
  10. // @author sp3x
  11. // @copyright sp3x / sp3x.de
  12. // @license All rights reserved
  13. // @version $Revision 1 $
  14. // @date $17.09.2009 $
  15. //
  16.  
  17. class Database
  18. {
  19.  
  20. //
  21. // MySQLi Object
  22. // @var mysqli
  23. //
  24. private $hMySQLi;
  25.  
  26. //
  27. // Debugging mode
  28. // @var boolean
  29. //
  30. private $debug;
  31.  
  32. //
  33. // MySQLi Result object
  34. // @var MySQLi::Result
  35. //
  36. private $result;
  37.  
  38. //
  39. // MySQLi Statement object
  40. // @var MySQLi::STMT
  41. //
  42. private $stmt;
  43.  
  44. //
  45. // Parameters for prepared statements
  46. // @var array
  47. //
  48. private $params;
  49.  
  50.     //
  51.     // Database constructor
  52.     // -----------------------------------------------------
  53.     // @param   boolean Debugging
  54.     //
  55.     public function __construct($debug = false)
  56.     {
  57.         $this->debug = $debug;
  58.     }
  59.    
  60.     //
  61.     // Connect to mysql database
  62.     // -----------------------------------------------------
  63.     // @param   string  Hostname
  64.     // @param   string  Username
  65.     // @param   string  Password
  66.     // @param   integer Port
  67.     // @param   boolean Persistente connection
  68.     //
  69.     public function connect($host = '127.0.0.1', $user = 'root', $pass = '', $port = 3306,
  70.         $pconn = false)
  71.     {
  72.         if($pconn)
  73.             $host = 'p:'.$host;
  74.         $this->hMySQLi = @new mysqli($host, $user, $pass, '', $port);
  75.         if(!$this->hMySQLi->connect_error)
  76.             return true;
  77.            
  78.         $this->debug();
  79.         return false;
  80.     }
  81.    
  82.     //
  83.     // Closes database connection an deletes mysqli object
  84.     // -----------------------------------------------------
  85.     //
  86.     public function disconnect()
  87.     {
  88.         @$this->hMySQLi->close();
  89.         unset($this->hMySQLi);
  90.         $this->hMySQLi  = null;
  91.     }
  92.    
  93.     //
  94.     // Returns error and errno if an error occurs
  95.     // -----------------------------------------------------
  96.     // @param   array   error[0] and errno[1]
  97.     // @return  mixed   Error or false on no error
  98.     //
  99.     public function error()
  100.     {
  101.         if($this->hMySQLi->connect_error)
  102.         {
  103.             return array($this->hMySQLi->connect_error, $this->hMySQLi->connect_errno, 'connect');
  104.         }
  105.        
  106.         if($this->hMySQLi->error)
  107.         {
  108.             return array($this->hMySQLi->error, $this->hMySQLi->errno, 'core');
  109.         }
  110.        
  111.         return false;
  112.     }
  113.    
  114.     //
  115.     // Selects specified default database for querys
  116.     // -----------------------------------------------------
  117.     // @param   string  Database name
  118.     // @return  boolean Database selection state
  119.     //
  120.     public function dbname($dbName)
  121.     {
  122.         $retVal = @$this->hMySQLi->select_db($dbName);
  123.         $this->debug();
  124.        
  125.         return $retVal;
  126.     }
  127.    
  128.     //
  129.     // -----------------------------------------------------
  130.     //
  131.     //
  132.     //
  133.     //
  134.     //
  135.     public function query($query = null)
  136.     {
  137.         if(is_array($query) || $query == null)
  138.         {
  139.            
  140.         } else
  141.         {
  142.             $this->stmt = $this->hMySQLi->query($query);
  143.         }
  144.         if($this->stmt)
  145.             return true;
  146.            
  147.         $this->debug();
  148.         return false;
  149.     }
  150.    
  151.     //
  152.     // -----------------------------------------------------
  153.     //
  154.     //
  155.     //
  156.     //
  157.     public function fetch()
  158.     {
  159.     }
  160.    
  161.     //
  162.     // -----------------------------------------------------
  163.     //
  164.     //
  165.     //
  166.     //
  167.     public function fetchAll()
  168.     {
  169.     }
  170.    
  171.     //
  172.     // Prepares query for execution
  173.     // -----------------------------------------------------
  174.     // @param   string  Query
  175.     // @return  boolean Prepare state
  176.     //
  177.     public function prepare($query)
  178.     {
  179.         if($this->stmt = $this->hMySQLi->prepare($query))
  180.             return true;
  181.            
  182.         $this->debug();
  183.         return false;
  184.     }
  185.    
  186.     //
  187.     // Starts transaction if database engine
  188.     // supports it (like InnoDB, NOT MyISAM)
  189.     // -----------------------------------------------------
  190.     //
  191.     public function transaction()
  192.     {
  193.         $this->hMySQLi->autocommit(false);
  194.        
  195.         $this->debug();
  196.     }
  197.    
  198.     //
  199.     // Rollbacks active transaction
  200.     // -----------------------------------------------------
  201.     //
  202.     public function rollback()
  203.     {
  204.         $this->hMySQLi->rollback();
  205.         $this->hMySQLi->autocommit(true);
  206.        
  207.         $this->debug();
  208.     }
  209.    
  210.     //
  211.     // Commits active transaction
  212.     // -----------------------------------------------------
  213.     //
  214.     public function commit()
  215.     {
  216.         $this->hMySQLi->commit();
  217.         $this->hMySQLi->autocommit(true);
  218.        
  219.         $this->debug();
  220.     }
  221.    
  222.     //
  223.     // Debugging function displays error messages if an error
  224.     // occurs
  225.     // -----------------------------------------------------
  226.     //
  227.     private function debug()
  228.     {
  229.         if($this->debug)
  230.         {
  231.             $error = $this->error();
  232.             if($error)
  233.             {
  234.                 echo    "<pre>MySQLi - Debugging Mode ({$error[2]}):\n------------------------\n";
  235.                 echo    "Error: {$error[0]}\nErrno: {$error[1]}\n\n</pre>";
  236.             }
  237.         }
  238.     }
  239.  
  240. }
  241.  
  242. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement