Guest User

Untitled

a guest
Jun 5th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.91 KB | None | 0 0
  1. <?php
  2.  
  3. // direct access not allowed
  4. if (!defined('ROOT')) die('Direct access not allowed.');
  5.  
  6. class Database {
  7.  
  8.     // debug flag for showing error messages
  9.     public  $debug = true;
  10.  
  11.     private $error = "";
  12.    
  13.     private $link_id = 0;
  14.     private $query_id = 0;
  15.    
  16.     public  $affected_rows = 0;
  17.    
  18.    
  19.     #-#############################################
  20.     # desc: constructor
  21.     function __construct($server=null, $user=null, $pass=null, $database=null)
  22.     {
  23.         // error catching if not passed in
  24.         if($server==null || $user==null || $database==null){
  25.             $this->oops("No mysql info");
  26.         }
  27.    
  28.         else
  29.         {
  30.             $this->link_id = @mysql_connect($server,$user,$pass);
  31.    
  32.             if (!$this->link_id){
  33.                 //open failed
  34.                 $this->oops("Could not connect to server: <b>".DB_SERVER."</b>.");
  35.             }
  36.    
  37.             if(!@mysql_select_db(DB_DATABASE, $this->link_id))
  38.             {
  39.                 //no database
  40.                 $this->oops("Could not open database: <b>".DB_DATABASE."</b>.");
  41.             }
  42.         }
  43.     }#-#constructor()
  44.    
  45.    
  46.     #-#############################################
  47.     # desc: close the connection
  48.     public function close(){
  49.         if(!@mysql_close($this->link_id)){
  50.             $this->oops("Connection close failed.");
  51.         }
  52.     }#-#close()
  53.    
  54.    
  55.     #-#############################################
  56.     # Desc: executes SQL query to an open connection
  57.     # Param: (MySQL query) to execute
  58.     # returns: (query_id) for fetching results etc
  59.     public function query($sql){
  60.         // do query
  61.         $this->query_id = @mysql_query($sql, $this->link_id);
  62.    
  63.         if (!$this->query_id){
  64.             $this->oops("<b>MySQL Query fail:</b> $sql");
  65.             return 0;
  66.         }
  67.        
  68.         $this->affected_rows = @mysql_affected_rows($this->link_id);
  69.    
  70.         return $this->query_id;
  71.     }#-#query()
  72.    
  73.    
  74.     #-#############################################
  75.     # desc: fetches and returns results one line at a time
  76.     # param: query_id for mysql run. if none specified, last used
  77.     # return: (array) fetched record(s)
  78.     public function fetch($query_id=-1){
  79.         // retrieve row
  80.         if ($query_id!=-1){
  81.             $this->query_id=$query_id;
  82.         }
  83.    
  84.         if (isset($this->query_id)){
  85.             $record = @mysql_fetch_assoc($this->query_id);
  86.         }else{
  87.             $this->oops("Invalid query_id: <b>$this->query_id</b>. Records could not be fetched.");
  88.         }
  89.    
  90.         return $record;
  91.     }#-#fetch()
  92.    
  93.    
  94.     #-#############################################
  95.     # desc: frees the resultset
  96.     # param: query_id for mysql run. if none specified, last used
  97.     private function free_result($query_id=-1){
  98.         if ($query_id!=-1){
  99.             $this->query_id=$query_id;
  100.         }
  101.         if($this->query_id!=0 && !@mysql_free_result($this->query_id)){
  102.             $this->oops("Result ID: <b>$this->query_id</b> could not be freed.");
  103.         }
  104.     }#-#free_result()
  105.    
  106.    
  107.     #-#############################################
  108.     # desc: throw an error message
  109.     # param: [optional] any custom error to display
  110.     private function oops($msg=''){
  111.         if(!empty($this->link_id)){
  112.             $this->error = mysql_error($this->link_id);
  113.         }
  114.         else{
  115.             $this->error = mysql_error();
  116.             $msg="<b>WARNING:</b> No link_id found. Likely not be connected to database.<br />$msg";
  117.         }
  118.    
  119.         // if no debug, done here
  120.         if(!$this->debug) return;
  121.         ?>
  122.             <table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
  123.             <tr><th colspan=2>Database Error</th></tr>
  124.             <tr><td align="right" valign="top">Message:</td><td><?php echo $msg; ?></td></tr>
  125.             <?php if(!empty($this->error)) echo '<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>'.$this->error.'</td></tr>'; ?>
  126.             <tr><td align="right">Date:</td><td><?php echo date("l, F j, Y \a\\t g:i:s A"); ?></td></tr>
  127.             <?php if(!empty($_SERVER['REQUEST_URI'])) echo '<tr><td align="right">Script:</td><td><a href="'.$_SERVER['REQUEST_URI'].'">'.$_SERVER['REQUEST_URI'].'</a></td></tr>'; ?>
  128.             <?php if(!empty($_SERVER['HTTP_REFERER'])) echo '<tr><td align="right">Referer:</td><td><a href="'.$_SERVER['HTTP_REFERER'].'">'.$_SERVER['HTTP_REFERER'].'</a></td></tr>'; ?>
  129.             </table>
  130.         <?php
  131.     }#-#oops()
  132.  
  133. }
  134. ?>
Add Comment
Please, Sign In to add comment