Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2. class engine
  3. {
  4.     private $hostname;
  5.     private $username;
  6.     private $password;
  7.     private $database;
  8.     public $connected;
  9.    
  10.     private $functions = array(
  11.         'connect'               =>              '@mysql_connect',
  12.         'select_db'             =>              '@mysql_select_db',
  13.         'close'                 =>              '@mysql_close',
  14.         'query'                 =>              '@mysql_query',
  15.         'escape_string'         =>              '@mysql_real_escape_string',
  16.         'num_rows'              =>              '@mysql_num_rows',     
  17.         'result'                =>              '@mysql_result',
  18.         'fetch_assoc'           =>              '@mysql_fetch_assoc',
  19.         'fetch_array'           =>              '@mysql_fetch_array'
  20.         );
  21.        
  22.     private $fetchT = array(
  23.         'both'                  =>              '@MYSQL_BOTH',
  24.         'assoc'                 =>              '@MYSQL_ASSOC'
  25.         );
  26.    
  27.     public function __construct($hostname, $username, $password, $database)
  28.     {
  29.         $this->hostname = $hostname;
  30.         $this->username = $username;
  31.         $this->password = $password;
  32.         $this->password = $password;
  33.        
  34.     }
  35.    
  36.    
  37.     final public function connect()
  38.     {
  39.         if(!$this->connected)
  40.         {
  41.             $myconnection = $this->functions['connect']($this->hostname, $this->username, $this->password);
  42.            
  43.             if($myconnection)
  44.             {
  45.                 $mydatabase = $this->functions['select_db']($this->database);
  46.                
  47.                 if($mydatabase)
  48.                 {
  49.                     $this->connected = true;
  50.                     return true;   
  51.                 }
  52.                 else
  53.                 {
  54.                     //Couldn't connect to database
  55.                 }
  56.             }
  57.             else
  58.             {
  59.                 //Couldn't connect to host
  60.             }
  61.         }
  62.         else
  63.         {
  64.             return true;
  65.         }
  66.     }
  67.    
  68.     final public function disconnect()
  69.     {
  70.         if($this->connected)
  71.         {
  72.             if($this->functions['close'])
  73.             {
  74.                 $this->connection = false;
  75.                 return true;
  76.             }
  77.             else
  78.             {
  79.                 //Couldn't disconnect
  80.             }
  81.         }
  82.     }
  83.    
  84.     final public function secure($var)
  85.     {
  86.         return $this->function['escape_string'](stripslashes(htmlspecialchars($var)));
  87.     }
  88.    
  89.     final public function select($table, $rows, $where = null, $order = null, $reason)//$engine->select('users','*','id='.$id,null,fetch_assoc)
  90.     {
  91.         if($this->connected)
  92.         {
  93.             $select = "SELECT " . $rows . " FROM " . $table;
  94.            
  95.             if($where != null)
  96.             {
  97.                 $select .= " WHERE " . $where;
  98.                 if($order != null)
  99.                 {
  100.                     $select .= " ORDER BY" . $order;
  101.                 }
  102.             }
  103.            
  104.             $select = $this->functions['query']($select);
  105.            
  106.             if($select)
  107.             {  
  108.                 if($reason)
  109.                 {
  110.                     switch($reason)
  111.                     {
  112.                         case 'num_rows':
  113.                             return $this->functions['num_rows']($select) or die(mysql_error());
  114.                         break;
  115.                        
  116.                         case 'fetch_assoc':
  117.                             return $this->functions['fetch_assoc']($select, $this->fetchT['both']) or die(mysql_error());
  118.                         break;
  119.                        
  120.                         case 'fetch_array':
  121.                             return $this->functions['fetch_array']($select, $this->fetchT['assoc']) or die(mysql_error());
  122.                         break;
  123.                        
  124.                         default:
  125.                             return false;
  126.                         break;
  127.                     }
  128.                 }
  129.                 else
  130.                 {
  131.                     //No reason specified must have a reason
  132.                 }
  133.             }
  134.         }
  135.     }
  136.    
  137.     final public function update($table, $row, $where = null)
  138.     {
  139.         if($this->connected)
  140.         {
  141.             $update = "UPDATE " . $table . " SET " . $row;
  142.            
  143.             if($where != null)
  144.             {
  145.                 $update .= "WHERE" . $where;
  146.             }
  147.            
  148.             return $this->functions['query']($update) or die(mysql_error());
  149.         }
  150.     }
  151.    
  152.     final public function insert($table, $rows = null, $values)
  153.     {
  154.         if($this->connected)
  155.         {
  156.             $insert = "INSERT INTO" . $table;
  157.            
  158.             if($rows != null)
  159.             {
  160.                 $insert .= "(" . $rows . ") VALUES(" . $values . ")";
  161.             }
  162.            
  163.             return $this->functions['query']($insert) or die(mysql_error());
  164.         }
  165.     }
  166.    
  167.     final public function delete($table, $where = null)
  168.     {
  169.         if($this->connected)
  170.         {
  171.             if($where != null)
  172.             {
  173.                 $delete = "DELETE FROM " . $table . " WHERE " . $where;
  174.             }
  175.             else
  176.             {
  177.                 $delete = "DELETE " . $table;
  178.             }
  179.            
  180.             return $this->functions['query']($delete);
  181.         }
  182.     }
  183. }
  184. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement