Advertisement
Guest User

Untitled

a guest
May 5th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.34 KB | None | 0 0
  1. Class MySQLiHandler {
  2.  
  3.     private $host = d_serv;
  4.     private $user = d_user;
  5.     private $pass = d_pass;
  6.     private $dbase = d_base;
  7.     private $connection;
  8.     private $num_rows="";
  9.    
  10.     protected function connect(){
  11.    
  12.         $this->connection = new mysqli($this->host,$this->user,$this->pass,$this->dbase);
  13.    
  14.         if( mysqli_connect_errno() ){
  15.        
  16.             $this->err_handler( 'database' , mysqli_connect_error() );
  17.        
  18.         }
  19.        
  20.         unset($this->host,$this->user,$this->pass,$this->dbase);
  21.    
  22.     }
  23.    
  24.     //////////////////////////////////////////////////////////////////////////
  25.     ///////////////////////  ESCAPE SQL VARIABLE v1.0  ///////////////////////
  26.     //////////////////////////////////////////////////////////////////////////
  27.     // --  DESCRIPTION  --------------------------------------------------- //
  28.     //                                                                      //
  29.     // Sends a variable to be preformatted to prevent SQL injection attacks //
  30.     //                                                                      //
  31.     // --  USAGE AND SYNTAX ----------------------------------------------- //
  32.     //                                                                      //
  33.     // Called via $object->escape($variable)                                //
  34.     // Returns the string but with escaped characters                       //
  35.     //                                                                      //
  36.     //////////////////////////////////////////////////////////////////////////
  37.    
  38.     protected function escape($variable) {
  39.    
  40.         return $this->connection->real_escape_string($variable);
  41.    
  42.     }
  43.  
  44.     //////////////////////////////////////////////////////////////////////////
  45.     //////////////////  SELECT (MULTIPLE ROWS) METHOD v1.1  //////////////////
  46.     //////////////////////////////////////////////////////////////////////////
  47.     // --  DESCRIPTION  --------------------------------------------------- //
  48.     //                                                                      //
  49.     // Passes a SELECT query to your database that fetches all rows of data //
  50.     // that matches your SQL query.                                         //
  51.     //                                                                      //
  52.     // The returned data is in the shape of an associative array where the  //
  53.     // first record holds the number of returned sets of data and the       //
  54.     // second row holds an associative array within another associative     //
  55.     // array with the result data from the query                            //
  56.     //                                                                      //
  57.     // --  USAGE AND SYNTAX ----------------------------------------------- //
  58.     //                                                                      //
  59.     // Called via $result = $object->select_m($sql)                         //
  60.     // Number of results returned is accessed via $result['count']          //
  61.     // Data is cycled through via => foreach( $result['data'] as $row) or   //
  62.      // accessed via => $result['data'][row_index][column_title]             //
  63.     //                                                                      //
  64.     //////////////////////////////////////////////////////////////////////////
  65.    
  66.     protected function select_m($sql){
  67.    
  68.         if ($fetch = $this->connection->query($sql)){
  69.            
  70.             $this->num_rows = mysqli_num_rows($fetch);
  71.            
  72.             while( $row = mysqli_fetch_assoc($fetch) ) {
  73.                 $row_array[] = $row;
  74.             }
  75.            
  76.             unset($sql);
  77.             $fetch->close();
  78.            
  79.             return array( "count"=> $this->num_rows , "data"=> $row_array );
  80.            
  81.         }
  82.  
  83.     }
  84.  
  85.     //////////////////////////////////////////////////////////////////////////
  86.     ////////////////////  SELECT (SINGLE ROW) METHOD v1.0  ///////////////////
  87.     //////////////////////////////////////////////////////////////////////////
  88.     // --  DESCRIPTION  --------------------------------------------------- //
  89.     //                                                                      //
  90.     // Passes a SELECT query to your database that fetches the first row of //
  91.     // data that matches your SQL query.                                    //
  92.     //                                                                      //
  93.     // The returned data is in the shape of an associative array where the  //
  94.     // first record holds the number of returned sets of data and the       //
  95.     // second row holds an associative array with the result data from the  //
  96.     // query.                                                               //
  97.     //                                                                      //
  98.     // --  USAGE AND SYNTAX ----------------------------------------------- //
  99.     //                                                                      //
  100.     // Called via $result->select_s($sql)                                   //
  101.     // Number of results returned is accessed via $result['count']          //
  102.     // Data is accessed via => $result['data']['column_title']              //
  103.     //                                                                      //
  104.     //////////////////////////////////////////////////////////////////////////
  105.    
  106.     protected function select_s($sql){
  107.    
  108.         if ($fetch = $this->connection->query($sql." LIMIT 1") ){
  109.            
  110.             $this->num_rows = mysqli_num_rows($fetch);
  111.             $row = mysqli_fetch_assoc($fetch);
  112.  
  113.             unset($sql);
  114.             $fetch->close();
  115.            
  116.             return array( "count"=> $this->num_rows , "data"=> $row );
  117.  
  118.         }
  119.  
  120.     }
  121.    
  122.     protected function disconnect(){
  123.    
  124.         $this->connection->close();
  125.    
  126.     }
  127.    
  128.     private function err_handler($type, $error=NULL){
  129.    
  130.         echo "Some squirrels got caught in the up machinery and created quite a mess!<br/>";
  131.         echo "We will clean this up as fast as we can so you can continue playing!";
  132.         die();
  133.    
  134.     }
  135.  
  136. }
  137.  
  138. Class SQLQuery extends MySQLiHandler {
  139.  
  140.     public $query_args = array();
  141.     private $class_query = "";
  142.     private $result = "";
  143.     private $escape_array = array();
  144.     private $default_select = "select_m";
  145.    
  146.     /* Calls Methods from MySQLiHandler */
  147.     public function call_parent($arg,$arg_data=NULL){
  148.    
  149.         if( !$arg_data == NULL ){
  150.        
  151.             return parent::$arg($arg_data);
  152.        
  153.         } else {
  154.        
  155.             return parent::$arg();
  156.        
  157.         }
  158.    
  159.     }
  160.    
  161.     public function set_defaults() {
  162.  
  163.         $this->query_args = array();
  164.         $this->class_query = "";
  165.         $this->result = "";
  166.         $this->escape_array = array();
  167.    
  168.     }
  169.    
  170.     // builds the query WHERE arguements and escapes them */
  171.     public function build_query_args($arg){
  172.    
  173.         foreach($arg as $key => $value){
  174.        
  175.             $this->query_args[$key] = parent::escape($value);
  176.        
  177.         }
  178.    
  179.     }
  180.    
  181.     //////////////////////////////////////////////////////////////////////////
  182.     //////  SQL queries for building the minimap on the city state page  /////
  183.     //////////////////////////////////////////////////////////////////////////
  184.  
  185.     public function sql_build_mm($s_method=$this->default_select) {
  186.    
  187.         $this->class_query = sprintf(
  188.         "SELECT
  189.         user.u_id AS u_id, citystates.c_id AS c_id,
  190.         minimap.mm_id AS mm_id, minimap.mm_xAxis AS mm_xAxis,
  191.         minimap.mm_yAxis AS mm_yAxis, minimap.mm_g_type AS mm_g_type,
  192.         gridtypes.g_icon AS g_icon
  193.  
  194.         FROM user
  195.  
  196.         FORCE INDEX(PRIMARY)
  197.  
  198.         JOIN
  199.         citystates ON User.u_id = citystates.c_u_id
  200.  
  201.         JOIN
  202.         minimap ON
  203.         citystates.c_xAxis = minimap.mm_MapX
  204.         AND
  205.         citystates.c_yAxis = minimap.mm_MapY
  206.  
  207.         JOIN
  208.         gridtypes ON
  209.         minimap.mm_g_type = gridtypes.g_id
  210.  
  211.         WHERE
  212.         user.u_id ='%s'
  213.         AND
  214.         citystates.c_xAxis ='%s'
  215.         AND
  216.         citystates.c_yAxis='%s'
  217.  
  218.         ORDER BY
  219.         mm_xAxis ASC , mm_yAxis DESC",
  220.         $this->query_args['user'],
  221.         $this->query_args['xaxis'],
  222.         $this->query_args['yaxis']
  223.         );
  224.        
  225.         return parent::$s_method($this->class_query);
  226.  
  227.     }
  228.  
  229.     ///////////////////////////////////////////////////////////////////////////
  230.     /////           SQL queries for handling the infobox content          /////
  231.     ///////////////////////////////////////////////////////////////////////////
  232.  
  233.     public function sql_get_grid($s_method=$this->default_select) {
  234.    
  235.         $this->class_query = sprintf(
  236.         "SELECT
  237.         gridtypes.g_name AS g_name
  238.        
  239.         FROM minimap
  240.        
  241.         FORCE INDEX(PRIMARY)
  242.        
  243.         JOIN gridtypes on minimap.mm_g_type = gridtypes.g_id
  244.        
  245.         WHERE
  246.         minimap.mm_xAxis='%s'
  247.         AND
  248.         minimap.mm_yAxis='%s'
  249.         AND
  250.         minimap.mm_mapX='%s'
  251.         AND
  252.         minimap.mm_mapY='%s'
  253.         ",
  254.         $this->query_args['x_axis'],
  255.         $this->query_args['y_axis'],
  256.         $this->query_args['parent_x'],
  257.         $this->query_args['parent_y']
  258.         );
  259.        
  260.         return parent::$s_method($this->class_query);
  261.        
  262.  
  263.     }
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement