class dbConn
{
var $link, $error;
var $host, $name, $user, $pass;
var $sql, $rows, $qry, $result;
var $obj, $row, $arr;
var $intLimit, $intOffset, $strType; function dbConn ( )
{
$this->error = new errorIndex();
}
// start a database connection using this function
function connect ( $user = 'groov3z_db', $pass = 'XXX', $host = 'db4.awardspace.com' )
{
$this->host = ( !$this->host ? $host : $this->host );
$this->user = ( !$this->user ? $user : $this->user );
$this->pass = ( !$this->pass ? $pass : $this->pass );
// try connecting to the specified database,
// link = true in case of success - dies and displays an error in case of failure
$this->link = @mysql_connect ( $this->host, $this->user, $this->pass );
if ( !is_resource ( $this->link ) )
{
$this->error->trigger( 0, mysql_error() );
return false;
}
return true;
}
// stop a database connection using this function
function close ( )
{
echo $this->error->display();
// try disconnecting from the database
// link = true in case of failure - false in case of success
return @mysql_close( $this->link );
}
// select the database you wish using this function
function select_db ( $name = false )
{
$this->name = $name;
// if a certain database is specified, try selecting it
// link remains true in case of success - dies and displays an error in case of failure
if ( $this->name )
{
if ( !@mysql_select_db ( $this->name, $this->link ) )
{
$this->error->trigger( 1, mysql_error() );
return false;
}
}
return true;
}
// execute a query on the database using this function
function qry ( $sql, $intLimit = false, $intOffset = false )
{
$this->sql = $sql;
$this->intLimit = $intLimit;
$this->intOffset = $intOffset;
// if a limit and/or offset are specified, rewrite the query
$this->sql .= ( $this->intLimit ? ( $this->intOffset ? ' LIMIT {$this->intLimit}, {$this->intOffset}' : ' LIMIT {$this->intLimit}' ) : '' );
// tries executing the query or dies and displays an error
if ( !$this->qry = @mysql_query( $this->sql, $this->link ) )
{
$this->error->trigger( 2, mysql_error() );
return false;
}
return true;
}
function result ( )
{
// check if a query was sent, otherwise display an error
$this->result = ( !$this->qry ? $this->error->trigger( 7, mysql_error() )
: @mysql_result( $this->qry ) );
return $this->result;
}
// fetch an object from a query using this function
function fetch_object ( )
{
// alright, try fetching the objects from the query,
// in case of failure die and display an error
if ( !$this->obj = @mysql_fetch_object ( $this->qry ) )
{
$this->error->trigger( 3, mysql_error() );
return false;
}
return $this->obj;
}
// fetch a row from a query using this function
function fetch_row ( )
{
// alright, try fetching the row from the query
// or otherwise die and display an error
if ( !$this->row = @mysql_fetch_row ( $this->qry ) )
{
$this->error->trigger( 4, mysql_error() );
return false;
}
return $this->row;
}
// fetch an array from a query using this function
function fetch_array ( $strType = 'MYSQL_BOTH' )
{
$this->strType = $strType;
// alright, try fetching the array from the query
// or die an display an error ofcourse in a case of failure!
if ( !$this->arr = @mysql_fetch_array ( $this->qry, $this->strType ) )
{
$this->error->trigger( 5, mysql_error() );
return false;
}
return $this->arr;
}
// get the number of rows (usually selected) using this function
function num_rows ( )
{
// assuming everything is ok,
// lets get the number of rows that we wanted
if ( !$this->rows = @mysql_num_rows ( $this->qry ) )
{
$this->error->trigger( 6, mysql_error() );
return false;
}
return $this->rows;
}
// ...
}