<?PHP
// MYSQL_CONNECTION: SQL database connection framework for MYSQL_DB
// Release 1.1 - 06/08/2002
// Released under GNU GPL (see LICENSE for details)
define ("LOG_NONE", 0) ;
define ("LOG_ECHO", 1) ;
define ("LOG_HIDE", 2) ;
define ("LOG_FILE", 3) ;
class mysql_conn {
// Init variables required
VAR $server = "" ;
VAR $user = "" ;
VAR $pwd = "" ;
VAR $database = "" ;
VAR $debuglv = 0 ;
// Data related variables
VAR $db = 0 ;
VAR $error_level = 0 ;
VAR $error_desc = "No errors" ;
VAR $logfile = "datalog" ;
VAR $filehdl = 0 ;
VAR $msg = "" ;
// --------- Private methods ---------
// Error reporting auxiliary method
function error_report()
{
$this->error_level = mysql_errno() ;
$this->error_desc = mysql_error() ;
}
// Opening the database connection
function connect_to_db()
{
// Try to connect to database server...
$this->db = mysql_connect($this->server,$this->user,$this->pwd) ;
$this->error_report() ;
if (!$this->db) {
// Failed to connect... abort connection process
$this->msg = date("d/m/Y - H:i:s") . " - ERROR " . $this->error_level . ": " . $this->error_desc . "\r\n" ;
$this->debug() ;
$this->release_db() ;
} else {
// Try to select a database...
if (mysql_select_db($this->database,$this->db)) {
// Everything is ready to work with the database
$this->msg = date("d/m/Y - H:i:s") . " - OPERATION O.K.: Connected to database " . $this->database . "\r\n";
} else {
// Failed to select the database... abort connection process
$this->error_report() ;
$this->msg = date("d/m/Y - H:i:s") . " - ERROR " . $this->error_level . ": " . $this->error_desc . "\r\n" ;
$this->debug() ;
$this->release_db() ;
}
}
}
// Releasing database connection
function release_db()
{
// Do we have a database open?
if ($this->db) {
// Must close the connection... not too important as php does by itself...
if (mysql_close($this->db)) {
$this->msg = date("d/m/Y - H:i:s") . " - OPERATION O.K.: Database " . $this->database . " released" . "\r\n";
} else {
// Failed to liberate the database...
$this->error_report() ;
$this->msg = date("d/m/Y - H:i:s") . " - ERROR " . $this->error_level . ": " . $this->error_desc . "\r\n" ;
}
} else {
// No database open
$this->msg = date("d/m/Y - H:i:s") . " - OPERATION CANCELLED: No database open" . "\r\n";
}
// LOG the operation and close logging operations
$this->debug() ;
$this->logfile_close() ;
}
// Log operations initialization
function logfile_init()
{
$fechagm = gmmktime()+3600 ;
$fecha = getdate($fechagm) ;
$this->msg = date("d/m/Y - H:i:s") . " ===== SESSION STARTED BY " . $GLOBALS["PHP_SELF"] . " =====" . "\r\n" ;
switch ($this->debuglv) {
case 0: // NO LOG OPERATIONS
break ;
case 1: // SCREEN OUTPUT
break ;
case 2: // SILENT OUTPUT (<!-- -->)
break ;
case 3: // FILE OUTPUT
$this->logfile = $this->logfile . "-" . $fecha["mon"] . "-" . $fecha["year"] ;
$this->filehdl = fopen($this->logfile,'a') ;
if (!$this->filehdl) {
echo "<!-- UNABLE TO OPEN SPECIFIED LOG FILE " . $this->logfile . " -->" ;
$this->debuglv-- ;
$this->logfile_init() ;
}
break ;
}
$this->debug() ;
}
// Closing log operations
function logfile_close()
{
if ($this->filehdl) {
// If we opened a file to log operations need to close it
fclose($this->filehdl) ;
}
}
// Debugging operations
function debug()
{
switch ($this->debuglv) {
case 0: // NO LOG OPERATIONS
break ;
case 1: // SCREEN OUTPUT
echo '<BR>DEBUG: ' . $this->msg . '<BR>' ;
break ;
case 2: // SILENT OUTPUT (<!-- -->)
echo "\n<!-- DEBUG: " . $this->msg . "-->\n" ;
break ;
case 3: // FILE OUTPUT
fwrite($this->filehdl,$this->msg) ;
break ;
}
}
// --------- Public interface ---------
// Initialization
function init()
{
$this->logfile_init() ;
$this->connect_to_db() ;
}
// Destructor
function destroy()
{
$this->release_db() ;
}
// Constructor
function mysql_conn($servername="localhost",$username,$password,$databasename,$debuglevel=0)
{
$this->server = $servername ;
$this->user = $username ;
$this->pwd = $password ;
$this->database = $databasename ;
$this->debuglv = $debuglevel ;
}
}
?>