Advertisement
Guest User

danii

a guest
Apr 7th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.22 KB | None | 0 0
  1. <?php
  2.  
  3. if ( !class_exists( 'DB' ) ) {
  4.     class DB {
  5.         /**
  6.          * @var null|mysqli
  7.          */
  8.         private $mysqli = null;
  9.         /**
  10.          * @var null|int
  11.          */
  12.         public $last_num_rows = null;
  13.         /**
  14.          * @var null|int
  15.          */
  16.         public $last_insert_id = null;
  17.  
  18.         /**
  19.          * @var mysqli_stmt[]
  20.          */
  21.         private $stmt_cache = array();
  22.  
  23.         /**
  24.          * DB constructor.
  25.          * @param $user
  26.          * @param $password
  27.          * @param $database
  28.          * @param string $host
  29.          */
  30.         public function __construct($user, $password, $database, $host = 'localhost') {
  31.             $this->user = $user;
  32.             $this->password = $password;
  33.             $this->database = $database;
  34.             $this->host = $host;
  35.         }
  36.  
  37.         /**
  38.          * @return mysqli
  39.          */
  40.         protected function connect() {
  41.             if( !$this->mysqli ){
  42.                 $this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->database);
  43.                 @$this->mysqli -> set_charset("utf8");
  44.             }
  45.             return $this->mysqli;
  46.         }
  47.  
  48.         /**
  49.          * @param $query
  50.          * @return array|false
  51.          */
  52.         public function query($query, $ClassName = null) {
  53.             $db = $this->connect();
  54.             $result = $db->query($query) or die( mysqli_error( $db ) );
  55.  
  56.             if( $result->num_rows > 0 ){
  57.                 $results = array();
  58.                 if( $ClassName ){
  59.                     while ( $row = $result->fetch_object($ClassName) ) {
  60.                         $results[] = $row;
  61.                     }
  62.                 }else{
  63.                     while ( $row = $result->fetch_assoc() ) {
  64.                         $results[] = $row;
  65.                     }
  66.                 }
  67.             }else{
  68.                 return false;
  69.             }
  70.             return $results;
  71.         }
  72.  
  73.         /**
  74.          * @param $table
  75.          * @param $data
  76.          * @param $format
  77.          * @return bool
  78.          */
  79.         public function insert($table, $data, $format) {
  80.             // Check for $table or $data not set
  81.             if ( empty( $table ) || empty( $data ) ) {
  82.                 return false;
  83.             }
  84.            
  85.             // Connect to the database
  86.             $db = $this->connect();
  87.            
  88.             // Cast $data and $format to arrays
  89.             $data = (array) $data;
  90.             $format = (array) $format;
  91.            
  92.             // Build format string
  93.             $format = implode('', $format);
  94.             $format = str_replace('%', '', $format);
  95.            
  96.             list( $fields, $placeholders, $values ) = $this->prep_query($data,$table);
  97.            
  98.             // Prepend $format onto $values
  99.             array_unshift($values, $format);
  100.             // Prepary our query for binding TODO: comment out the mysqli_error
  101.             $stmt = $this->prepare($db, "INSERT INTO {$table} ({$fields}) VALUES ({$placeholders})");
  102.             // Dynamically bind values
  103.             call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($values));
  104.            
  105.             // Execute the query
  106.             $stmt->execute() or die( mysqli_error( $db ));
  107.            
  108.             // Check for successful insertion
  109.             if ( $stmt->affected_rows ) {
  110.                 $this->last_insert_id = $stmt->insert_id;
  111.                 return true;
  112.             }
  113.            
  114.             return false;
  115.         }
  116.  
  117.         /**
  118.          * @param $table
  119.          * @param $data
  120.          * @param $format
  121.          * @param $where
  122.          * @param $where_format
  123.          * @return bool
  124.          */
  125.         public function update($table, $data, $format, $where, $where_format) {
  126.             // Check for $table or $data not set
  127.             if ( empty( $table ) || empty( $data ) ) {
  128.                 return false;
  129.             }
  130.            
  131.             // Connect to the database
  132.             $db = $this->connect();
  133.            
  134.             // Cast $data and $format to arrays
  135.             $data = (array) $data;
  136.             $format = (array) $format;
  137.            
  138.             // Build format array
  139.             $format = implode('', $format);
  140.             $format = str_replace('%', '', $format);
  141.             $where_format = implode('', $where_format);
  142.             $where_format = str_replace('%', '', $where_format);
  143.             $format .= $where_format;
  144.            
  145.             list( $fields, $placeholders, $values ) = $this->prep_query($data, $table, 'update');
  146.            
  147.             //Format where clause
  148.             $where_clause = '';
  149.             $where_values = '';
  150.             $count = 0;
  151.            
  152.             foreach ( $where as $field => $value ) {
  153.                 if ( $count > 0 ) {
  154.                     $where_clause .= ' AND ';
  155.                 }
  156.                
  157.                 $where_clause .= $field . '=?';
  158.                 $where_values[] = $value;
  159.                
  160.                 $count++;
  161.             }
  162.             // Prepend $format onto $values
  163.             array_unshift($values, $format);
  164.             $values = array_merge($values, $where_values);
  165.             // Prepary our query for binding
  166.             $stmt = $this->prepare($sql, "UPDATE {$table} SET {$placeholders} WHERE {$where_clause}");
  167.            
  168.             // Dynamically bind values
  169.             call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($values));
  170.            
  171.             // Execute the query
  172.             $stmt->execute();
  173.            
  174.             // Check for successful insertion
  175.             if ( $stmt->affected_rows ) {
  176.                 return true;
  177.             }
  178.            
  179.             return false;
  180.         }
  181.  
  182.         /**
  183.          * @param $query
  184.          * @param $data
  185.          * @param $format
  186.          * @param $ClassName
  187.          * @return array
  188.          */
  189.         public function select($query, $data, $format, $ClassName = null) {
  190.             // Connect to the database
  191.             $db = $this->connect();
  192.            
  193.             //Prepare our query for binding
  194.             $stmt = $this->prepare($db, $query);
  195.            
  196.             //Normalize format
  197.             $format = implode('', $format);
  198.             $format = str_replace('%', '', $format);
  199.            
  200.             // Prepend $format onto $values
  201.             array_unshift($data, $format);
  202.            
  203.             //Dynamically bind values
  204.             call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($data));
  205.            
  206.             //Execute the query
  207.             $stmt->execute();
  208.            
  209.             //Fetch results
  210.             $result = $stmt->get_result();
  211.  
  212.             $this->last_num_rows = $result->num_rows;
  213.  
  214.             if( $result->num_rows ){
  215.                 $results = array();
  216.                 if( $ClassName ){
  217.                     while ($row = $result->fetch_object($ClassName)) {
  218.                         $results[] = $row;
  219.                     }
  220.                 }else{
  221.                     while ($row = $result->fetch_assoc()) {
  222.                         $results[] = $row;
  223.                     }
  224.                 }
  225.             }else{
  226.                 $results = false;
  227.             }
  228.             return $results;
  229.         }
  230.  
  231.         /**
  232.          * @param $query
  233.          * @param $data
  234.          * @param $format
  235.          * @return bool
  236.          */
  237.         public function delete($query, $data, $format) {
  238.             // Connect to the database
  239.             $db = $this->connect();
  240.  
  241.             //Prepare our query for binding
  242.             $stmt = $this->prepare($db, $query);
  243.  
  244.             //Normalize format
  245.             $format = implode('', $format);
  246.             $format = str_replace('%', '', $format);
  247.  
  248.             // Prepend $format onto $values
  249.             array_unshift($data, $format);
  250.  
  251.             //Dynamically bind values
  252.             call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($data));
  253.  
  254.             // Execute the query
  255.             $stmt->execute();
  256.  
  257.             // Check for successful insertion
  258.             if ( $stmt->affected_rows ) {
  259.                 return true;
  260.             }
  261.             return false;
  262.         }
  263.  
  264.         public function SpecialPrepared($query,$data,$format){
  265.             // Connect to the database
  266.             $db = $this->connect();
  267.  
  268.             // Prepare our query for binding
  269.             $stmt = $this->prepare($db, $query);
  270.  
  271.             //Normalize format
  272.             $format = implode('', $format);
  273.             $format = str_replace('%', '', $format);
  274.  
  275.             // Prepend $format onto $values
  276.             array_unshift($data, $format);
  277.  
  278.             //print_r( $data );
  279.             // Dynamically bind values
  280.             call_user_func_array( array( $stmt, 'bind_param'), $this->ref_values($data));
  281.  
  282.             // Execute the query
  283.             $stmt->execute() or die( mysqli_error( $db ) );
  284.  
  285.             // Check for successful query
  286.             if ( $stmt->affected_rows ) {
  287.                 return true;
  288.             }
  289.             return false;
  290.         }
  291.         /**
  292.          * @param $data
  293.          * @param string $type
  294.          * @return array
  295.          */
  296.         private function prep_query($data, $table, $type='insert') {
  297.             // Instantiate $fields and $placeholders for looping
  298.             $fields = '';
  299.             $placeholders = '';
  300.             $values = array();
  301.             $table = $table.".";
  302.  
  303.             // Loop through $data and build $fields, $placeholders, and $values        
  304.             foreach ( $data as $field => $value ) {
  305.                 $field = $table.$field;
  306.                 $fields .= "{$field},";
  307.                 $values[] = $value;
  308.                
  309.                 if ( $type == 'update') {
  310.                     $placeholders .= $field . '=?,';
  311.                 } else {
  312.                     $placeholders .= '?,';
  313.                 }
  314.                
  315.             }
  316.            
  317.             // Normalize $fields and $placeholders for inserting
  318.             $fields = substr($fields, 0, -1);
  319.             $placeholders = substr($placeholders, 0, -1);
  320.            
  321.             return array( $fields, $placeholders, $values );
  322.         }
  323.  
  324.         /**
  325.          * @param $array
  326.          * @return array
  327.          */
  328.         private function ref_values($array) {
  329.             $refs = array();
  330.             foreach ($array as $key => $value) {
  331.                 $refs[$key] = &$array[$key];
  332.             }
  333.             return $refs;
  334.         }
  335.  
  336.         private function prepare($db, $query) {
  337.             $key = md5($query);
  338.            
  339.             if (!isset($this->stmt_cache[$key])) {
  340.                 $this->stmt_cache[$key] = $db->prepare($query) or die( mysqli_error( $db ) );
  341.             }
  342.  
  343.             return $this->stmt_cache[$key];
  344.         }
  345.     }
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement