Advertisement
Guest User

Untitled

a guest
May 30th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.29 KB | None | 0 0
  1.         function getInstance(){
  2.             static $instance;
  3.             if(!isset($instance)){
  4.                 $object= __CLASS__;
  5.                 $instance=new $object;
  6.             }
  7.             return $instance;
  8.         }
  9.         /**
  10.          * @since 1.0
  11.          * Simple constructor
  12.          * @return
  13.          */            
  14.         function db(){//connection object
  15.             $this -> linkid = mysql_connect($this->host,$this->user,$this->password);
  16.                 mysql_select_db($this->dbase,$this -> linkid); 
  17.             //$this -> connect();
  18.         }
  19.         /**
  20.          *
  21.          * Used to override the database connection
  22.          *
  23.          * @return Void
  24.          * @param string $user (Database Username)
  25.          * @param string $password (Database Password)
  26.          * @param string $host (Database Host)
  27.          * @param string $dbase (Name of the database you wish to connect to)
  28.          */
  29.         public function Connect ($user,$password,$host,$dbase){ //allow for new connections to be made
  30.                 $this->user = $user;
  31.                 $this->password= $password;
  32.                 $this->host = $host;
  33.                 $this->dbase= $dbase;
  34.                 $this -> linkid = mysql_connect($this->host,$this->user,$this->password);
  35.                 mysql_select_db($this->dbase,$this -> linkid); 
  36.         }
  37.         /**
  38.          * Changes the Database the queries are run against
  39.          *
  40.          * @since 2.0
  41.          * @return
  42.          * @param object $dbName
  43.          */
  44.         public function Selectdb($dbName){mysql_select_db($dbName,$this->linkid);}
  45.         /**
  46.          * Counts the amount of rows returned
  47.          *
  48.          * @since 1.0
  49.          * @return int
  50.          */
  51.         public function Count_res(){return mysql_num_rows($this -> Resid);  }
  52.  
  53.         /**
  54.          * Runs the query passed to it.
  55.          * @since 1.0
  56.          * @return int|string|bool (You should never get back true, its just a catch all)
  57.          * @param string $sql[optional] not really optional.  Will just run a blank query.  Useful for testing
  58.          * @param bool $cache[optional] defaults to false
  59.          */
  60.         public function Query($sql = "",$cache = false){
  61.             if($this -> linkid != 0){
  62.                 //mysql_ping($this -> linkid);
  63.                 $sql = $this->prefixParse($sql); //Lets check to see if the prefix string is being used
  64.                 if($this -> Cache_all === true || $cache === true){
  65.                     if(strpos(strtolower($sql),"select") == 0){
  66.                         str_replace("select","SELECT SQL_CACHE",strtolower($sql));
  67.                     }
  68.                 }
  69.                 $return = mysql_query($sql,$this -> linkid) or $return = FALSE;
  70.                 $this -> Resid = $return;
  71.                 $this -> Lastsql = $sql;
  72.                 if(!$return){//set the error values
  73.                     $this -> Error["Query"] = $this -> Lastsql;
  74.                     $this -> Error["Error"] = mysql_error();
  75.                     $return = "There was an error with your sql";
  76.                 }else { $this -> Error = array();
  77.                     $this -> Queries++;
  78.                     if(strpos(strtolower($sql),"insert") == 0){//this was an insert
  79.                         $this -> Lastid = mysql_insert_id($this -> linkid); }
  80.                 }
  81.                 return true; //Something always has to be returned
  82.             }
  83.         }
  84.         /**
  85.          * Fetch and Format do the same thing, Fetch is just a wrapper for Format to be more MySQL/PHP standard
  86.          *
  87.          * @since 1.5
  88.          * @see function Format
  89.          * @return string
  90.          * @param string $type
  91.          * @param bool $force[optional]
  92.          */
  93.         public function Fetch($type,$force = FALSE){ return $this->Format($type,$force);} // to be more like mysql
  94.         /**
  95.          * Used to save stored resluts.  Can be dangerous and should be used with caution.
  96.          * @return int|string
  97.          * @param mixed $results
  98.          * @param string $index[optional]
  99.          */
  100.         public function Store_results($results,$index=""){
  101.             if($index==""){
  102.                 if($this->storeResults[] = $results){return count($this->storeresults);}else{return false;}
  103.             }else{
  104.                 if($this->storeResults[$index] = $results){return $index;}else{return false;}
  105.             }
  106.         }
  107.         /**
  108.          * Gets the results
  109.          * @return mixed the array of results stored.  Could be a string as well its all up to the user
  110.          * @param int|string $id
  111.          */
  112.         public function Get_results($id){
  113.             if(array_key_exists($id,$this->storeResults)){return $this->storeResults[$id];}else{return false;}
  114.         }
  115.         /**
  116.          * A clean up of the stored results
  117.          * @return bool Incase of need to validate
  118.          */
  119.         public function Clear_savedresults(){ if($this->storeresults = array()){return true;} return false;}
  120.         /**
  121.          * Format takes the Query (if it was successful) and then turns it into an Array, associative or indexed (assoc/row/assoc_array/row_array)
  122.          *
  123.          * @since 1.0
  124.          * @return mixed
  125.          * @param string $type
  126.          * @param bool $force[optional]
  127.          */
  128.         public function Format($type,$force = false){
  129.             $return = "";
  130.             if(count($this -> Error) == 2){//there is an error
  131.                 return "There was an error with the query";
  132.             }else{
  133.                 switch(strtolower($type)){
  134.                     case "assoc":
  135.                         if($this -> Count_res() == 1 || $force === true)
  136.                             $return = mysql_fetch_assoc($this -> Resid);
  137.                         else                   
  138.                             while($line = mysql_fetch_assoc($this -> Resid)){ $return[] = $line; }
  139.                     break;
  140.                     case "row":
  141.                         if($this -> Count_res() == 1 || $force === true){
  142.                             $return = mysql_fetch_row($this -> Resid);
  143.                         }else{
  144.                             while($line = mysql_fetch_row($this -> Resid)){ $return[] = $line; }}
  145.                             if($this->Count_res() > 0){
  146.                                 if(count($return) == 1) {$return = $return[0];} //make sure its more than one
  147.                             }
  148.                     break;
  149.                     case "assoc_array":
  150.                         while($line = mysql_fetch_assoc($this -> Resid)){ $return[] = $line; }
  151.                     break;
  152.                     case "row_array":
  153.                         while($line = mysql_fetch_row($this -> Resid)){ $return[] = $line; }
  154.                         if(count($return) == 1) {return $return;} //make sure its more than one
  155.                         if(count($return[0])==1){foreach($return as $r){$newArray[]=$r[0];} $return = $newArray;}
  156.                     break;
  157.                     default:
  158.                         $return = "broke";
  159.                     break;
  160.                 }
  161.                 if(count($return)==0) {$return = array();}
  162.                 return $return;
  163.             }
  164.         }
  165.         /**
  166.          * Used to clean and escape a string for entering into a SQL query
  167.          *
  168.          * @since 1.5
  169.          * @return string
  170.          * @param string $item
  171.          */
  172.         public function Escape_str($item){foreach ($item as $key=>$i){$return[] = "'".mysql_real_escape_string($i)."'"; }return $return;}
  173.         /**
  174.          * Used to clean and escape an Array for entering into a SQL query
  175.          *
  176.          * @since 1.5
  177.          * @return string
  178.          * @param mixed $array
  179.          */
  180.         public function Mysql_clean($array){$holderArray=array();foreach ($array as $key => $value){$holderArray[$key] = mysql_real_escape_string($value);} return $holderArray;}
  181.  
  182.         /**
  183.          * Cleans a string.  More robust than the 2 above
  184.          *
  185.          * @return array,string The cleaned up version of what was passed to it
  186.          * @param array,str $str[optional] what is to be cleaned
  187.          * @param array string $html[optional] tags to be removed
  188.          */
  189.         public function Clean($str = '', $html = false) {
  190.             if (empty($str)) return;
  191.        
  192.             if (is_array($str)) {
  193.                 foreach($str as $key => $value) $str[$key] = $this->clean($value, $html);
  194.             } else {
  195.                 if (get_magic_quotes_gpc()){ $str = stripslashes($str);}else{$str = addslashes($str);}
  196.        
  197.                 if (is_array($html)) $str = strip_tags($str, implode('', $html));
  198.                 elseif (preg_match('|<([a-z]+)>|i', $html)) $str = strip_tags($str, $html);
  199.                 elseif ($html !== true) $str = strip_tags($str);
  200.        
  201.                 $str = trim($str);
  202.             }
  203.        
  204.             return $str;
  205.         }
  206.         /**
  207.          * Returns the SQL query with the prefix variable string exchanged for the prefix
  208.          *
  209.          * @since 2.0
  210.          * @return string
  211.          * @param string $sql
  212.          */
  213.         private function prefixParse($sql){ return str_replace($this->prefixString,$this->Prefix,$sql);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement