Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class MySQLiHandler {
- private $host = d_serv;
- private $user = d_user;
- private $pass = d_pass;
- private $dbase = d_base;
- private $connection;
- private $num_rows="";
- protected function connect(){
- $this->connection = new mysqli($this->host,$this->user,$this->pass,$this->dbase);
- if( mysqli_connect_errno() ){
- $this->err_handler( 'database' , mysqli_connect_error() );
- }
- unset($this->host,$this->user,$this->pass,$this->dbase);
- }
- //////////////////////////////////////////////////////////////////////////
- /////////////////////// ESCAPE SQL VARIABLE v1.0 ///////////////////////
- //////////////////////////////////////////////////////////////////////////
- // -- DESCRIPTION --------------------------------------------------- //
- // //
- // Sends a variable to be preformatted to prevent SQL injection attacks //
- // //
- // -- USAGE AND SYNTAX ----------------------------------------------- //
- // //
- // Called via $object->escape($variable) //
- // Returns the string but with escaped characters //
- // //
- //////////////////////////////////////////////////////////////////////////
- protected function escape($variable) {
- return $this->connection->real_escape_string($variable);
- }
- //////////////////////////////////////////////////////////////////////////
- ////////////////// SELECT (MULTIPLE ROWS) METHOD v1.1 //////////////////
- //////////////////////////////////////////////////////////////////////////
- // -- DESCRIPTION --------------------------------------------------- //
- // //
- // Passes a SELECT query to your database that fetches all rows of data //
- // that matches your SQL query. //
- // //
- // The returned data is in the shape of an associative array where the //
- // first record holds the number of returned sets of data and the //
- // second row holds an associative array within another associative //
- // array with the result data from the query //
- // //
- // -- USAGE AND SYNTAX ----------------------------------------------- //
- // //
- // Called via $result = $object->select_m($sql) //
- // Number of results returned is accessed via $result['count'] //
- // Data is cycled through via => foreach( $result['data'] as $row) or //
- // accessed via => $result['data'][row_index][column_title] //
- // //
- //////////////////////////////////////////////////////////////////////////
- protected function select_m($sql){
- if ($fetch = $this->connection->query($sql)){
- $this->num_rows = mysqli_num_rows($fetch);
- while( $row = mysqli_fetch_assoc($fetch) ) {
- $row_array[] = $row;
- }
- unset($sql);
- $fetch->close();
- return array( "count"=> $this->num_rows , "data"=> $row_array );
- }
- }
- //////////////////////////////////////////////////////////////////////////
- //////////////////// SELECT (SINGLE ROW) METHOD v1.0 ///////////////////
- //////////////////////////////////////////////////////////////////////////
- // -- DESCRIPTION --------------------------------------------------- //
- // //
- // Passes a SELECT query to your database that fetches the first row of //
- // data that matches your SQL query. //
- // //
- // The returned data is in the shape of an associative array where the //
- // first record holds the number of returned sets of data and the //
- // second row holds an associative array with the result data from the //
- // query. //
- // //
- // -- USAGE AND SYNTAX ----------------------------------------------- //
- // //
- // Called via $result->select_s($sql) //
- // Number of results returned is accessed via $result['count'] //
- // Data is accessed via => $result['data']['column_title'] //
- // //
- //////////////////////////////////////////////////////////////////////////
- protected function select_s($sql){
- if ($fetch = $this->connection->query($sql." LIMIT 1") ){
- $this->num_rows = mysqli_num_rows($fetch);
- $row = mysqli_fetch_assoc($fetch);
- unset($sql);
- $fetch->close();
- return array( "count"=> $this->num_rows , "data"=> $row );
- }
- }
- protected function disconnect(){
- $this->connection->close();
- }
- private function err_handler($type, $error=NULL){
- echo "Some squirrels got caught in the up machinery and created quite a mess!<br/>";
- echo "We will clean this up as fast as we can so you can continue playing!";
- die();
- }
- }
- Class SQLQuery extends MySQLiHandler {
- public $query_args = array();
- private $class_query = "";
- private $result = "";
- private $escape_array = array();
- private $default_select = "select_m";
- /* Calls Methods from MySQLiHandler */
- public function call_parent($arg,$arg_data=NULL){
- if( !$arg_data == NULL ){
- return parent::$arg($arg_data);
- } else {
- return parent::$arg();
- }
- }
- public function set_defaults() {
- $this->query_args = array();
- $this->class_query = "";
- $this->result = "";
- $this->escape_array = array();
- }
- // builds the query WHERE arguements and escapes them */
- public function build_query_args($arg){
- foreach($arg as $key => $value){
- $this->query_args[$key] = parent::escape($value);
- }
- }
- //////////////////////////////////////////////////////////////////////////
- ////// SQL queries for building the minimap on the city state page /////
- //////////////////////////////////////////////////////////////////////////
- public function sql_build_mm($s_method=$this->default_select) {
- $this->class_query = sprintf(
- "SELECT
- user.u_id AS u_id, citystates.c_id AS c_id,
- minimap.mm_id AS mm_id, minimap.mm_xAxis AS mm_xAxis,
- minimap.mm_yAxis AS mm_yAxis, minimap.mm_g_type AS mm_g_type,
- gridtypes.g_icon AS g_icon
- FROM user
- FORCE INDEX(PRIMARY)
- JOIN
- citystates ON User.u_id = citystates.c_u_id
- JOIN
- minimap ON
- citystates.c_xAxis = minimap.mm_MapX
- AND
- citystates.c_yAxis = minimap.mm_MapY
- JOIN
- gridtypes ON
- minimap.mm_g_type = gridtypes.g_id
- WHERE
- user.u_id ='%s'
- AND
- citystates.c_xAxis ='%s'
- AND
- citystates.c_yAxis='%s'
- ORDER BY
- mm_xAxis ASC , mm_yAxis DESC",
- $this->query_args['user'],
- $this->query_args['xaxis'],
- $this->query_args['yaxis']
- );
- return parent::$s_method($this->class_query);
- }
- ///////////////////////////////////////////////////////////////////////////
- ///// SQL queries for handling the infobox content /////
- ///////////////////////////////////////////////////////////////////////////
- public function sql_get_grid($s_method=$this->default_select) {
- $this->class_query = sprintf(
- "SELECT
- gridtypes.g_name AS g_name
- FROM minimap
- FORCE INDEX(PRIMARY)
- JOIN gridtypes on minimap.mm_g_type = gridtypes.g_id
- WHERE
- minimap.mm_xAxis='%s'
- AND
- minimap.mm_yAxis='%s'
- AND
- minimap.mm_mapX='%s'
- AND
- minimap.mm_mapY='%s'
- ",
- $this->query_args['x_axis'],
- $this->query_args['y_axis'],
- $this->query_args['parent_x'],
- $this->query_args['parent_y']
- );
- return parent::$s_method($this->class_query);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement