<?
/*******************************************
USAGE: $My_db=new base_mysql($DB_HostName, $DB_user, $DB_Password, $DB_Name);
$My_db->base_Open();
$My_db->base_Query($my_query);
... your code ...
$My_db->base_Close();
or
$My_db=new base_mysql($DB_HostName, $DB_user, $DB_Password, "");
$My_db->base_Open();
$My_db->base_Select_DB($DB_Name);
$My_db->base_Query($my_query);
... your code ...
$My_db->base_Select_DB($OtherDB_Name);
etc.
********************************************/
class base_mysql {
/* public variables*/
/* private variables */
var $_DB_Name;
var $_DB_User;
var $_DB_Pass;
var $_DB_Host;
var $_DB_Link;
var $_query_ID;
var $_row;
var $_debug;
var $_error_handling;
var $_stop_on_error;
/* private constants*/
var $_terminated;
/********* CLASS CONSTRUCTOR *********/
function base_mysql ($Host_DB="localhost", $User_DB="root", $Pass_DB="",$Name_DB="")
{
$this->base_Continue();
$this->base_errors();
$this->base_Debug();
$this->_DB_Name = $Name_DB;
$this->_DB_User = $User_DB;
$this->_DB_Pass = $Pass_DB;
$this->_DB_Host = $Host_DB;
$this->_DB_link = 0;
$this->_query_ID = 0;
$this->_terminated = "<br><b>!!!SCRIPT TERMINATED CAUSE ERROR!!!</b>";
}
/********* SETTINGS *********/
function base_Debug($ON_OFF = False)
/*
ON_OFF = False - deactivate debug messages.
ON_OFF = True - activate debug messages.
*/
{
$this->_debug=$ON_OFF;
}
function base_errors($ON_OFF = True)
/*
ON_OFF = False - deactivate error handling.
ON_OFF = True - activate error handling.
*/
{
$this->_error_handling=$ON_OFF;
}
function base_Continue($ON_OFF = True)
/*
ON_OFF = False - stop script after an error.
ON_OFF = True - don't stop script after an error.
*/
{
$this->_stop_on_error = $ON_OFF;
}
/********* CLASS METHODS *********/
function base_Open()
{
if ($this->_debug)
{ echo "<br><i>Hostname: <b>$this->_DB_Host</b><br>";
echo "User: <b>$this->_DB_User</b></i><br>";
}
if (($this->_DB_User=="") || ($this->_DB_Host=="")) {$this->_errors(1);}
else
{
$this->_DB_Link = mysql_connect ($this->_DB_Host, $this->_DB_User, $this->_DB_Pass);
if (!$this->_DB_Link)
{$this->_errors();}
else
{ if ($this->_debug==True) {echo "<br><i>Connection to DB server <b>$this->_DB_Host</b> active.</i><br>";}
if ($this->_DB_Name!="") {$this->base_Select_DB($this->_DB_Name);}
}
}
}
function base_Close()
{
if ($this->_DB_Link)
{
if ($this->_debug==True) {echo "<br><i>Closing connection to DB server <b>$this->_DB_Host</b>.</i><br>";}
if (!mysql_close($this->_DB_Link)) $this->_errors();
}
}
function base_Select_DB ($other_DB_Name="")
{
if ($other_DB_Name=="") {$this->_errors(2);}
else
{
if ($this->_DB_Link==0) {$this->_errors(4);}
else
{
if (mysql_select_db($other_DB_Name, $this->_DB_Link))
{$this->_DB_Name = $other_DB_Name;
if ($this->_debug==True) {echo "<br><i>Database <b>$this->_DB_Name</b> selected.</i><br>";}
}
else {$this->_errors();}
}
}
}
function base_Get_DB()
{ /* returns the current selected DB or empty if none */
return $this->_DB_Name;
}
function base_Query($_sql)
{ /* execute a query */
if (!$this->_DB_Link) {$this->_errors(4);}
else
{
if ($this->_debug==True) {echo"<br><i>Executing query</i>: <b>$_sql<b>.<br>";}
$this->_query_ID = mysql_query ($_sql, $this->_DB_Link);
if (!$this->_query_ID)
{$this->_errors();
unset ($_sql);
return False;}
else
{unset ($_sql);
return True;}
}
}
function base_Num_Rows()
{
if ($this->_query_ID!=0)
{
$Number = mysql_num_rows($this->_query_ID);
if (is_null($Number)){return "NULL";}
else {return $Number;}
}
else
{
$this->_errors(3);
return False;
}
}
function base_Affected_Rows()
{
if ($this->_DB_Link!=0)
{
$Number = mysql_affected_rows($this->_DB_Link);
if (is_null($Number)){return "NULL";}
else {return $Number;}
}
else
{
$this->_errors(4);
return False;
}
}
function base_Data_Seek($row_number=0)
{
if ($this->_query_ID!=0)
{ mysql_data_seek($this->_query_ID,$row_number);}
else
{
$this->_errors(3);
return False;
}
}
function base_Fetch_Row()
{
if ($this->_query_ID!=0)
{ return mysql_fetch_row($this->_query_ID);}
else
{
$this->_errors(3);
return False;
}
}
/********* ERROR HANDLING FUNCTION *********/
function _errors ($int_err=0)
{if (!$this->_error_handling) {return False;}
else
{
switch ($int_err)
{
case 0: /* for My_SQL errors handling */
echo "<br><b>Error number:</b> $mysql_errno()<br>";
echo "<b>Error Text:</b> $mysql_error()<br>";
break;
case 1:
echo "<br><b>Please specify User/Host!</b><br>";
break;
case 2:
echo "<br><b>Database name not set.</b><br>";
break;
case 3:
echo "<br><b>No valid query have been executed.</b><br>";
break;
case 4:
echo "<br><b>No DB connection available.</b><br>";
break;
default:
break;
}
if (!$this->_stop_on_error) die($this->_terminated);
}
}
/********* OTHER FUNCTIONS *********/
function _is_boolean($_to_check)
{/* for future use */
if (is_bool($_to_check))
{return True;}
else
{$this->_errors(4);
return False;}
}
}
?> |