Advertisement
Guest User

Untitled

a guest
May 30th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.83 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.          * @param bool $fetch[optional] if passed will call format with the value.
  60.          * @param bool $force[optional] see format for definition
  61.          * @param string $fetchId [optional] the id field to set as each key of a multidementional array
  62.          */
  63.         public function Query($sql = "",$cache = false,$fetch=false,$force=false,$fetchId=false){
  64.             if($this -> linkid != 0){
  65.                 //mysql_ping($this -> linkid);
  66.                 $sql = $this->prefixParse($sql); //Lets check to see if the prefix string is being used
  67.                 if($this -> Cache_all === true || $cache === true){
  68.                     if(strpos(strtolower($sql),"select") == 0){
  69.                         str_replace("select","SELECT SQL_CACHE",strtolower($sql));
  70.                     }
  71.                 }
  72.                 $return = mysql_query($sql,$this -> linkid) or $return = FALSE;
  73.                 $this -> Resid = $return;
  74.                 $this -> Lastsql = $sql;
  75.                 if(!$return){//set the error values
  76.                     $this -> Error["Query"] = $this -> Lastsql;
  77.                     $this -> Error["Error"] = mysql_error();
  78.                     $return = "There was an error with your sql";
  79.                 }else { $this -> Error = array();
  80.                     $this -> Queries++;
  81.                     if(strpos(strtolower($sql),"insert") == 0){//this was an insert
  82.                         $this -> Lastid = mysql_insert_id($this -> linkid); }
  83.                 }
  84.                 if(!$fetch){
  85.                     return true; //Something always has to be returned 
  86.                 }else{
  87.                     return $this->Fetch($fetch,$force,$fetchId);
  88.                 }
  89.                
  90.             }
  91.         }
  92.         /**
  93.          * Fetch and Format do the same thing, Fetch is just a wrapper for Format to be more MySQL/PHP standard
  94.          *
  95.          * @since 1.5
  96.          * @see function Format
  97.          * @return string
  98.          * @param string $type
  99.          * @param bool $force[optional]
  100.          */
  101.         public function Fetch($type,$force = FALSE,$idField="id"){ return $this->Format($type,$force,$idField);} // to be more like mysql
  102.         /**
  103.          * Used to save stored resluts.  Can be dangerous and should be used with caution.
  104.          * @return int|string
  105.          * @param mixed $results
  106.          * @param string $index[optional]
  107.          */
  108.         public function Store_results($results,$index=""){
  109.             if($index==""){
  110.                 if($this->storeResults[] = $results){return count($this->storeresults);}else{return false;}
  111.             }else{
  112.                 if($this->storeResults[$index] = $results){return $index;}else{return false;}
  113.             }
  114.         }
  115.         /**
  116.          * Gets the results
  117.          * @return mixed the array of results stored.  Could be a string as well its all up to the user
  118.          * @param int|string $id
  119.          */
  120.         public function Get_results($id){
  121.             if(array_key_exists($id,$this->storeResults)){return $this->storeResults[$id];}else{return false;}
  122.         }
  123.         /**
  124.          * A clean up of the stored results
  125.          * @return bool Incase of need to validate
  126.          */
  127.         public function Clear_savedresults(){ if($this->storeresults = array()){return true;} return false;}
  128.         /**
  129.          * Format takes the Query (if it was successful) and then turns it into an Array, associative or indexed (assoc/row/assoc_array/row_array)
  130.          *
  131.          * @since 1.0
  132.          * @return mixed
  133.          * @param string $type
  134.          * @param bool $force[optional]
  135.          */
  136.         public function Format($type,$force = false,$idField = "id"){
  137.             $return = "";
  138.             if(count($this -> Error) == 2){//there is an error
  139.                 return "There was an error with the query";
  140.             }else{
  141.                 switch(strtolower($type)){
  142.                     case "assoc":
  143.                         if($this -> Count_res() == 1 || $force === true)
  144.                             $return = mysql_fetch_assoc($this -> Resid);
  145.                         else                   
  146.                             while($line = mysql_fetch_assoc($this -> Resid)){ $return[] = $line; }
  147.                     break;
  148.                     case "row":
  149.                         if($this -> Count_res() == 1 || $force === true){
  150.                             $return = mysql_fetch_row($this -> Resid);
  151.                         }else{
  152.                             while($line = mysql_fetch_row($this -> Resid)){ $return[] = $line; }}
  153.                             if($this->Count_res() > 0){
  154.                                 if(count($return) == 1) {$return = $return[0];} //make sure its more than one
  155.                             }
  156.                     break;
  157.                     case "assoc_array":
  158.                         while($line = mysql_fetch_assoc($this -> Resid)){
  159.                             if(isset($line[$idField])){
  160.                                 $return[$line[$idField]] = $line;  
  161.                             }else{
  162.                                 $return[] = $line; 
  163.                             }
  164.                          
  165.                         }
  166.                     break;
  167.                     case "row_array":
  168.                         while($line = mysql_fetch_row($this -> Resid)){ $return[] = $line; }
  169.                         if(count($return) == 1) {return $return;} //make sure its more than one
  170.                         if(count($return[0])==1){foreach($return as $r){$newArray[]=$r[0];} $return = $newArray;}
  171.                     break;
  172.                     default:
  173.                         $return = "broke";
  174.                     break;
  175.                 }
  176.                 if(count($return)==0) {$return = array();}
  177.                 return $return;
  178.             }
  179.         }
  180.         /**
  181.          * Used to clean and escape a string for entering into a SQL query
  182.          *
  183.          * @since 1.5
  184.          * @return string
  185.          * @param string $item
  186.          */
  187.         public function Escape_str($item){foreach ($item as $key=>$i){$return[] = "'".mysql_real_escape_string($i)."'"; }return $return;}
  188.         /**
  189.          * Used to clean and escape an Array for entering into a SQL query
  190.          *
  191.          * @since 1.5
  192.          * @return string
  193.          * @param mixed $array
  194.          */
  195.         public function Mysql_clean($array){$holderArray=array();foreach ($array as $key => $value){$holderArray[$key] = mysql_real_escape_string($value);} return $holderArray;}
  196.  
  197.         /**
  198.          * Cleans a string.  More robust than the 2 above
  199.          *
  200.          * @return array,string The cleaned up version of what was passed to it
  201.          * @param array,str $str[optional] what is to be cleaned
  202.          * @param array string $html[optional] tags to be removed
  203.          */
  204.         public function Clean($str = '', $html = false) {
  205.             if (empty($str)) return;
  206.        
  207.             if (is_array($str)) {
  208.                 foreach($str as $key => $value) $str[$key] = $this->clean($value, $html);
  209.             } else {
  210.                 if (get_magic_quotes_gpc()){ $str = stripslashes($str);}else{$str = addslashes($str);}
  211.        
  212.                 if (is_array($html)) $str = strip_tags($str, implode('', $html));
  213.                 elseif (preg_match('|<([a-z]+)>|i', $html)) $str = strip_tags($str, $html);
  214.                 elseif ($html !== true) $str = strip_tags($str);
  215.        
  216.                 $str = trim($str);
  217.             }
  218.        
  219.             return $str;
  220.         }
  221.         /**
  222.          * Returns the SQL query with the prefix variable string exchanged for the prefix
  223.          *
  224.          * @since 2.0
  225.          * @return string
  226.          * @param string $sql
  227.          */
  228.         private function prefixParse($sql){ return str_replace($this->prefixString,$this->Prefix,$sql);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement