Advertisement
Guest User

Untitled

a guest
May 18th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.01 KB | None | 0 0
  1. <?php
  2.  
  3. class Database {
  4.    
  5.     var $server;
  6.     var $username;
  7.     var $password;
  8.     var $database;
  9.    
  10.     // Exceptions
  11.     var $errno  = '0';
  12.     var $error  =   '0';
  13.    
  14.     // Link/Query Id's
  15.     var $affected_rows  =   '0';
  16.     var $link_id    = '0';
  17.     var $query_id   =   '0';
  18.  
  19.     function __construct($server,$username,$password) {
  20.         $this->server   = $server;
  21.         $this->username =   $username;
  22.         $this->password =   $password;
  23.     }
  24.    
  25.     public function connect() {
  26.         if ($this->link_id = mysql_connect($this->server,$this->username,$this->password))
  27.             return true;
  28.         else
  29.             $this->exception("Could not connect to ".$this->server);
  30.            
  31.         $this->server = "";
  32.         $this->username = "";
  33.         $this->password = "";
  34.     }
  35.    
  36.     public function changeDB($database) {
  37.         if (!empty($database)) {
  38.             $this->database =   $database;
  39.         }
  40.         if (mysql_select_db($this->database))
  41.             return true;
  42.         else
  43.             $this->exception("Could not switch database to ".$this->database);
  44.     }
  45.    
  46.     public function close() {
  47.         if(!@mysql_close($this->link_id)){
  48.             $this->oops("Connection close failed.");
  49.         }
  50.     }
  51.    
  52.     public function escape($string) {
  53.         if(get_magic_quotes_runtime()) $string = stripslashes($string);
  54.         return @mysql_real_escape_string($string,$this->link_id);
  55.     }
  56.    
  57.     public function query($sql) {
  58.         $this->query_id = @mysql_query($sql,$this->link_id);
  59.        
  60.         if (!$this->query_id)   {
  61.             $this->exception("Could not run query. SQL: ".$sql);
  62.             return false;
  63.         }
  64.        
  65.         $this->affected_rows = @mysql_affected_rows($this->link_id);
  66.        
  67.         return $this->query_id;
  68.     }
  69.    
  70.     function fetch_array($query_id=-1) {
  71.         if ($query_id!=-1) {
  72.             $this->query_id=$query_id;
  73.         }
  74.    
  75.         if (isset($this->query_id)) {
  76.             $record = @mysql_fetch_object($this->query_id);
  77.         }else{
  78.             $this->exception("Invalid query_id: <b>".$this->query_id."</b>. Records could not be fetched.");
  79.         }
  80.    
  81.         return $record;
  82.     }
  83.    
  84.     public function query_update($table,$data,$where = '1') {
  85.         $q = "UPDATE `".$table."` SET ";
  86.        
  87.         foreach ($data as $key => $value) {
  88.             switch (strtolower($value)) {
  89.                 case "null":
  90.                     $q .= $key." = NULL,";
  91.                     break;
  92.                 case "now()":
  93.                     $q .= $key." = NOW(),";
  94.                     break;
  95.                 default:
  96.                     $q .= $key." = ".$this->escape($value).",";
  97.                     break;
  98.             }
  99.         }
  100.            
  101.         $q = rtrim($q, ', ') . ' WHERE '.$where.';';
  102.            
  103.         return $this->query($q);
  104.                
  105.     }
  106.            
  107.     public function query_insert($table,$data) {
  108.         $q = "INSERT INTO `".$table."` ";
  109.         $k = "";$v = "";
  110.        
  111.     foreach($data as $key => $val) {
  112.         $k.="`$key`, ";
  113.         if(strtolower($val)=='null') $v.="NULL, ";
  114.         elseif(strtolower($val)=='now()') $v.="NOW(), ";
  115.         else $v.= "'".$this->escape($val)."', ";
  116.     }
  117.  
  118.     $q .= "(". rtrim($k, ', ') .") VALUES (". rtrim($v, ', ') .");";
  119.  
  120.     if($this->query($q)){
  121.         return mysql_insert_id($this->link_id);
  122.     }
  123.     else return false;
  124.   }
  125.  
  126.   private function exception($msg = '') {
  127.     if($this->link_id>0){
  128.         $this->error=mysql_error($this->link_id);
  129.         $this->errno=mysql_errno($this->link_id);
  130.     }
  131.     else{
  132.         $this->error=mysql_error();
  133.         $this->errno=mysql_errno();
  134.     }
  135.     ?>
  136.         <table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
  137.         <tr><th colspan=2>Database Error</th></tr>
  138.         <tr><td align="right" valign="top">Message:</td><td><?php echo $msg; ?></td></tr>
  139.         <?php if(strlen($this->error)>0) echo '<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>'.$this->error.'</td></tr>'; ?>
  140.         <tr><td align="right">Date:</td><td><?php echo date("l, F j, Y \a\\t g:i:s A"); ?></td></tr>
  141.         <tr><td align="right">Script:</td><td><a href="<?php echo @$_SERVER['REQUEST_URI']; ?>"><?php echo @$_SERVER['REQUEST_URI']; ?></a></td></tr>
  142.         <?php if(strlen(@$_SERVER['HTTP_REFERER'])>0) echo '<tr><td align="right">Referer:</td><td><a href="'.@$_SERVER['HTTP_REFERER'].'">'.@$_SERVER['HTTP_REFERER'].'</a></td></tr>'; ?>
  143.         </table>
  144.     <?php  
  145.   }
  146. }
  147.  
  148. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement