PHP Classes
Icontem

File: ibclass.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Martin Fasani  >  Interbase with error logging  >  ibclass.php  
File: ibclass.php
Role: Class source
Content type: text/plain
Description: Class source. Additional data handling functions included.
Class: Interbase with error logging
Interbase access wrapper. Conn/Query errors logged
 

Contents

Class file image Download
<?php
/* - - - - - - - - - - - - - - - - - - 
 * Based on http://www.phpclasses.org/browse/file/1431.html: Vinicius Gallafrio BR (24/01/2002)
 * Modified by martinfasani@gmail.com
 * Date : 13/04/2005
   E.g:
 * include "ibclass.php";
 * $ib = New ib('/path/to/database.gdb','sysdba','masterkey',0);
 * $ib->query("SELECT * FROM table");                        ^-- change to 1 for debug mode
 * 
*/
/* <INTERBASE PUBLIC FUNCTIONS >*/

class ib {
  
/* public: connection parameters */
  
var $debug;
  var 
$database;
  var 
$user;
  var 
$password;
  
/* private: connection parameters */
  
var $conn;
  var 
$result;
  
//in spanish       "Dom","Lun","Mar","Mie","Jue","Vie","Sab"
  
var $dayweek=array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
/** interbase::interbase()
 * Constructor this class - define public connection parameters and call the connect method
 * 
 * @param $database
 * @param $user
 * @param $password
 * @param $debug
 */
  
function ib($database,$user,$password,$debug=0) {
    
$this->debug $debug;
    
//if ($this->debug) echo "\n\nDebug On <br>\n";        
     
$this->user $user;
     
$this->password $password;
     
$this->database $database;
     
// open connection
     
$this->connect();
    }

 
/**
 * interbase::connect()
 * Open connection with the server
 * @return conn id
 */
  
function connect () { 
   
$this->conn = @ibase_connect($this->database,$this->user,$this->password);    
   if ( 
ibase_errmsg() ) {
     
$report ="PAGE:  $_SERVER[REQUEST_URI] \r\n ERRORMSG:  ".ibase_errmsg(); 
     if (
$this->debug) { 
     die(
"<font color=red>$report</font><br><br>Connecting to ".$this->database);
     } else {
     
$connlogfile="ibconnlog.txt";
     
//Open the file in append mode and write log
     
$fp fopen($connlogfile,a);
     
// Write $log to our opened file. 
     
$log=$_SERVER["REMOTE_ADDR"]." | ".date ("l dS of F Y h:i:s A")." | "ibase_errmsg()." \n";
      if (!
fwrite($fp$log)) { 
       print 
"Cannot write to file ($connlogfile)"
       exit; 
      } 
     
fclose($fp); 
     die(
"<font color=red><br>La connectividad con base de datos ha fallado. Se ha creado un registro del error y se ha enviado al equipo técnico. Por favor inténtelo mas tarde.</font>");
     }
   }
   return 
$this->conn;                
  }

function 
disconnect() { 
  return 
ibase_close($this->conn);
}
  
// ibquery with error reporting to table STABUG 
  
function query ($query="") { 
   if (empty(
$query)) return false
   
$this->result = @ibase_query($query,$this->conn);
   if ( 
ibase_errmsg() ) {
     
$report ="PAGE:  $_SERVER[REQUEST_URI] \r\n ERRORMSG:  ".ibase_errmsg(); 
     if (
$this->debug) {  /* Debug: Dies with DB error & query */
       
die("<font color=red>$report</font><br><br>".$query);
 
     } else {       
/* No debug: Writes a row in STABUG and shows message */
      
$bugquery    $this->ibvalstring($query);
      
$ibase_errmsgibase_errmsg();
      
$buglog "INSERT INTO BUGLOG (BUGURI,BUGMSG,BUGQRY,BUGSTA) VALUES ('$_SERVER[REQUEST_URI]','ibase_errormsg: $ibase_errmsg','$bugquery',current_timestamp)"
      
$result = @ibase_query($buglog) or die ($buglog);
      die(
"<font color=red><br>La consulta a la base de datos ha fallado. Se ha creado un registro del error y se ha enviado al equipo técnico. Por favor inténtelo mas tarde.</font>");
     }
    }
   return 
$this->result;
  }

  function 
free_result () { 
   return 
ibase_free_result($this->result);
  }
  
// Replaces [\r\n] and single quotes for ibase
  
function ibvalstring($query) {
   
$search  = array ("'([\r\n])[\s]+'");
   
$replace = array ("");
   
$query   preg_replace($search$replace$query); 
   
$query   str_replace("'""''"$query);
   return 
$query;
  }

  
// DATE functions .STORED IN IB numeric  YYYYMMDD
  
function ibtonorm($dt) { // IN: YYYYMMDD  OUT: DD/MM/YYYY
   
$yr=strval(substr($dt,0,));
   
$mo=strval(substr($dt,4,));
   
$da=strval(substr($dt,6,));          
   return 
$da."/".$mo."/".$yr
  } 
  function 
normtoib($dt) {  // IN: DD/MM/YYYY OUT: YYYYMMDD
   
$da=strval(substr($dt,0,)); 
   
$mo=strval(substr($dt,3,));
   
$yr=strval(substr($dt,6,));         
   return 
"$yr$mo$da"
  } 
  function 
ibtostamp($dt) { // IN: MM/DD/YYYY HH:MM:SS OUT: YYYYMMDDHHMMSS
   
$mo=strval(substr($dt,0,)); 
   
$da=strval(substr($dt,3,));
   
$yr=strval(substr($dt,6,));         
   
$hh=strval(substr($dt,11,)); 
   
$mm=strval(substr($dt,14,));
   
$ss=strval(substr($dt,17,));    
   return 
"$yr$mo$da$hh$mm$ss"
  } 
  function 
ibgetday($dt) {  // IN:YYYYMMDD  OUT: Lun /Mar
   
$wn=date ("w"strtotime($dt) );
   return 
$this->dayweek[$wn];
  }

}
/* </INTERBASE PUBLIC FUNCTIONS> */
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products