Advertisement
Guest User

Untitled

a guest
May 9th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.23 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 23 $
  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 $hResult;
  37.  
  38. //
  39. // MySQLi Statement object
  40. // @var MySQLi::STMT
  41. //
  42. private $hStmt;
  43.  
  44. //
  45. // Parameters for prepared statements
  46. // @var array
  47. //
  48. private $params;
  49.  
  50. //
  51. // Table prefix
  52. // @var string
  53. //
  54. private $pref;
  55.  
  56. //
  57. // Bound assoc columns
  58. // @var boolean
  59. //
  60. private $bound;
  61.  
  62.     //
  63.     // Database constructor
  64.     // -----------------------------------------------------
  65.     // @param   boolean Debugging
  66.     //
  67.     public function __construct($debug = false)
  68.     {
  69.         $this->debug = $debug;
  70.     }
  71.    
  72.     //
  73.     // Connect to mysql database
  74.     // -----------------------------------------------------
  75.     // @param   string  Hostname
  76.     // @param   string  Username
  77.     // @param   string  Password
  78.     // @param   integer Port
  79.     // @param   boolean Persistente connection
  80.     //
  81.     public function connect($host = '127.0.0.1', $user = 'root', $pass = '', $port = 3306,
  82.         $pref = '', $pconn = false)
  83.     {
  84.         $this->pref = $pref;
  85.        
  86.         if($pconn)
  87.             $host = 'p:'.$host; // Use persistent connection so prefix host with "p:"
  88.         $this->hMySQLi = @new mysqli($host, $user, $pass, '', $port);
  89.         if(!$this->hMySQLi->connect_error)
  90.             return true;
  91.            
  92.         $this->debug();
  93.         return false;
  94.     }
  95.    
  96.     //
  97.     // Closes database connection an deletes mysqli object
  98.     // -----------------------------------------------------
  99.     //
  100.     public function disconnect()
  101.     {
  102.         @$this->hMySQLi->close();
  103.         $this->hMySQLi  = null;
  104.         unset($this->hMySQLi);
  105.     }
  106.    
  107.     //
  108.     // Returns error and errno if an error occurs
  109.     // -----------------------------------------------------
  110.     // @param   array   error[0] and errno[1]
  111.     // @return  mixed   Error or false on no error
  112.     //
  113.     public function error()
  114.     {
  115.         // Returns errors occurs on establish connection to mysqld
  116.         if($this->hMySQLi->connect_error)
  117.         {
  118.             return array($this->hMySQLi->connect_error, $this->hMySQLi->connect_errno, 'connect');
  119.         }
  120.        
  121.         // Returns errors occurs on mysql usage
  122.         if($this->hMySQLi->error)
  123.         {
  124.             return array($this->hMySQLi->error, $this->hMySQLi->errno, 'core');
  125.         }
  126.        
  127.         // Returns errors occurs while executing stmt's
  128.         if(is_object($this->hStmt) && $this->hStmt->error)
  129.         {
  130.             return array($this->hStmt->error, $this->hStmt->errno, 'stmt');
  131.         }
  132.        
  133.         return false;
  134.     }
  135.    
  136.     //
  137.     // Selects specified default database for querys
  138.     // -----------------------------------------------------
  139.     // @param   string  Database name
  140.     // @return  boolean Database selection state
  141.     //
  142.     public function dbname($dbName)
  143.     {
  144.         $retVal = @$this->hMySQLi->select_db($dbName);
  145.         $this->debug();
  146.        
  147.         return $retVal;
  148.     }
  149.    
  150.     //
  151.     // Executes a query on the mysql server
  152.     // -----------------------------------------------------
  153.     // @param   mixed   Query-String or null on prepared statement
  154.     // @param   mixed   Prepared statement parameter
  155.     // @return  boolean True on success, false on fail
  156.     //
  157.     public function query($query = null, &$args = null)
  158.     {
  159.         if(($query == null) && is_object($this->hStmt))
  160.         {
  161.             // Build parameter array for bind_result
  162.             if(is_array($args))
  163.             {
  164.                 $params[0]  = '';
  165.                 $count      = count($args);
  166.                 for($i=0; $i<$count; $i++)
  167.                 {
  168.                     $params[0]  .= $args[$i][0];
  169.                     $params[]   = &$args[$i][1];
  170.                 }
  171.                 call_user_func_array(array($this->hStmt, 'bind_param'), $params);
  172.             }
  173.             // Execute prepared statement
  174.             $this->hResult  = $this->hStmt->execute();
  175.         } else
  176.         {   // Execute simple statement
  177.             $this->resetStmt();
  178.             $this->hResult  = $this->hMySQLi->query($query, MYSQLI_USE_RESULT);
  179.         }
  180.         if($this->hStmt)
  181.             return true;
  182.            
  183.         $this->debug();
  184.         return false;
  185.     }
  186.    
  187.     //
  188.     // Fetch result as assoc array
  189.     // -----------------------------------------------------
  190.     // @return  mixed   Assoc-Array or FALSE
  191.     //
  192.     public function fetch()
  193.     {
  194.         if(is_object($this->hStmt))
  195.         {
  196.             // Bind columns as references fpr assoc result
  197.             if($this->bound === false)
  198.             {
  199.                 $fields     = array();
  200.                 $meta       = $this->hStmt->result_metadata();
  201.                 $columns    = $meta->fetch_fields();
  202.                 foreach($columns as $column)
  203.                 {
  204.                     $fields[]   = &$this->bound[$column->name];
  205.                 }
  206.                 call_user_func_array(array($this->hStmt, 'bind_result'), $fields);
  207.             }
  208.            
  209.             // Fillout assoc result
  210.             if($this->hStmt->fetch())
  211.                 return $this->bound;
  212.         } elseif($this->hResult)
  213.         {
  214.             return $this->hResult->fetch_assoc();
  215.         }
  216.        
  217.         return false;
  218.     }
  219.    
  220.     //
  221.     // Fetch all rows into one array
  222.     // -----------------------------------------------------
  223.     // @return  mixed   Assoc-Array or FALSE
  224.     //
  225.     public function fetchAll()
  226.     {
  227.         $result = array();
  228.         while($result[] = $this->fetch())
  229.         if(count($result))
  230.             return $result;
  231.            
  232.         return false;
  233.     }
  234.    
  235.     //
  236.     // Prepares query for execution
  237.     // -----------------------------------------------------
  238.     // @param   string  Query
  239.     // @return  boolean Prepare state
  240.     //
  241.     public function prepare($query)
  242.     {
  243.         $this->resetStmt();
  244.         if($this->hStmt = $this->hMySQLi->prepare($query))
  245.             return true;
  246.            
  247.         $this->debug();
  248.         return false;
  249.     }
  250.    
  251.     //
  252.     // Starts transaction if database engine
  253.     // supports it (like InnoDB, NOT MyISAM)
  254.     // -----------------------------------------------------
  255.     //
  256.     public function transaction()
  257.     {
  258.         $this->resetStmt();
  259.         // Disable autocommit so transactions are not commited automatically (Transaction begin)
  260.         $this->hMySQLi->autocommit(false);
  261.        
  262.         $this->debug();
  263.     }
  264.    
  265.     //
  266.     // Rollbacks active transaction
  267.     // -----------------------------------------------------
  268.     //
  269.     public function rollback()
  270.     {
  271.         $this->hMySQLi->rollback();
  272.         // Re-Enable autocommit (Transaction end)
  273.         $this->hMySQLi->autocommit(true);
  274.        
  275.         $this->debug();
  276.     }
  277.    
  278.     //
  279.     // Commits active transaction
  280.     // -----------------------------------------------------
  281.     //
  282.     public function commit()
  283.     {
  284.         $this->hMySQLi->commit();
  285.         // Re-Enable autocommit (Transaction end)
  286.         $this->hMySQLi->autocommit(true);
  287.        
  288.         $this->debug();
  289.     }
  290.    
  291.     //
  292.     // Frees result and stmt objects used memory
  293.     // -----------------------------------------------------
  294.     //
  295.     private function resetStmt($null = true)
  296.     {
  297.         if(is_object($this->hStmt))
  298.         {
  299.             // Reset statement-obj if initialized
  300.             @$this->hStmt->free_result();
  301.             @$this->hStmt->close();
  302.             if($null)
  303.                 $this->hStmt    = null;
  304.         } elseif(is_object($this->hResult))
  305.         {
  306.             // Reset result-obj
  307.             @$this->hResult->free();
  308.             @$this->hResult->close();
  309.             if($null)
  310.                 $this->hStmt    = null;
  311.         }
  312.         $this->bound    = false;
  313.     }
  314.    
  315.     //
  316.     // Debugging function displays error messages if an error
  317.     // occurs
  318.     // -----------------------------------------------------
  319.     //
  320.     private function debug()
  321.     {
  322.         if($this->debug)
  323.         {
  324.             $error = $this->error();
  325.             if($error)
  326.             {
  327.                 echo "MySQLi - Debugging Mode ({$error[2]}):\n----------------------------------\n";
  328.                 echo "Error: {$error[0]}\nErrno: {$error[1]}\n\n";
  329.             }
  330.         }
  331.     }
  332.  
  333. }
  334.  
  335. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement