Advertisement
Guest User

Untitled

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