MySQL Database Php

Database Connection Class
**************
What it's for:
This Class Opens a Database Connection. It's useful if you need to make more
than 1 SQL Statement in a single PHP Script.
Use it with ReUseConn in the MySQLDatabase class.
*/
class DBConnection
{
var $iConnectId;
/**
Function: Open
**************
Parameters:
$szDatabase == Name of the Database to connect to.
$szHost == Name of the Host on which szDatabase resides. Usually localhost
$szUser == Name of User to connect as.
$szPassword == Password of the User.
Return:
Return true if everything went well else it will return false.
What it's for:
This function actually opens the Database Connection.
*/
function Open( $szDatabase, $szHost, $szUser, $szPassword )
{
$this->iConnectId = false;
$iRet = true;
$this->iConnectId = mysql_connect( $szHost , $szUser, $szPassword );
if( $this->iConnectId )
$iRet = mysql_select_db( $szDatabase, $this->iConnectId );
else
$iRet = false;
return $iRet;
/*echo "DBConnection";*/
}
/****
Function: Connection
**************
Parameters:
None
Return:
Returns the ConnectionID of the current Databae connection if a
Database COnnection has been established else it will return false.
What it's for:
This function retruns the Database ConnectionID. Use can pass this
functions return Value to the ReUseConn function of the MySQL Database Class.
**/
function Connection()
{
return $this->iConnectId;
}
}
/**
MySQL Database Class
**************
What it's for:
This class is used to Communicate with a mySQL Database Server.
*/
class MySQLDatabase
{
// Class Variables
var $iResultId;
var $szSQLString;
var $iNumRows;
var $iCurrentRow;
var $arRow;
/**
Function: Open
**************
Parameters:
$szDatabase == Name of the Database to connect to.
$szHost == Name of the Host on which szDatabase resides. Usually localhost
$szUser == Name of User to connect as.
$szPassword == Password of the User.
Return:
Return true if everything went well else it will return false.
What it's for:
This function actually opens the Database Connection.
*/
function Open( $szDatabase, $szHost, $szUser, $szPassword )
{
$this->iConnectId = false;
$this->szSQLString = "";
$this->iResultId = false;
$this->iNumRows = false;
$this->arRow = false;
$this->iCurrentRow = 0;
$iRet = true;
$this->iConnectId = mysql_connect( $szHost , $szUser, $szPassword );
if( $this->iConnectId )
{ $iRet = mysql_select_db( $szDatabase, $this->iConnectId );
if( !$iRet )
echo "Selected Database doesn't exist!!!";
}
else
{ echo "Connect didn't work out!!!";
$iRet = false;
}
return $iRet;
}
/*
Function: ReUseConn
**************
Parameters:
$iDBConnection == A valid ConnectionID to a mySQL Database.
Return:
None
What it's for:
This function reuses an already established Database connection.
*/
function ReUseConn( $iDBConnection )
{
$this->iConnectId = false;
$this->szSQLString = "";
$this->iResultId = false;
$this->iNumRows = false;
$this->arRow = false;
$this->iCurrentRow = 0;
$this->iConnectId = $iDBConnection;
}
/**
Function: Query
**************
Parameters:
$szSelect == This parameter is a little misnamed. It should probably be called
$szQueryString
Return:
true on ok.
false on error.
What it's for:
This function will actually Query the Database and set or reset some internal
Variables which are needed.
*/
function Query( $szSelect )
{
$this->szSQLString = "";
$this->szSQLString = $szSelect;
$this->iResultId = false;
$this->iNumRows = false;
$this->arRow = false;
$this->iCurrentRow = 0;
$iRet = true;
if( $this->iConnectId != 0)
{
if( strlen( $this->szSQLString ) > 0 )
{
$this->iResultId = mysql_query( $this->szSQLString, $this->iConnectId );
if( $this->iResultId )
{
$tok = strtok($this->szSQLString," ");
$tok = strtoupper( $tok );
if( !strcmp( $tok, "SELECT" ) )
$this->iNumRows = mysql_num_rows( $this->iResultId );
else
$this->iNumRows = mysql_affected_rows( $this->iResultId );
}
else
{
//echo "Error no ResultIndex!!!";
$iRet = false;
}
}
else
{
echo "Open called without any SQL Query!!!";
$iRet = false;
}
}
return $iRet;
}
/*
Function: NumRows
**************
Parameters:
None
Return:
The number of Rows affected by the last Query..
What it's for:
This function will return the number of affected Rows. Regardless if the
last Query was a SELECT, UPDATE, DELETE, or INSERT Statment..
*/
function NumRows()
{
if( $this->iConnectId )
return $this->iNumRows;
else
return false;
}
/**
Function: MoveTo
**************
Parameters:
int $iRow == The Row to move to.
Return:
true on success, else false.
What it's for:
Moves the recordset to the specified row in the resultset.
!!!! I implemented this function but haven't tested it yet!!!! *sorry*
*/
function MoveTo( $iRow )
{
if( $this->iConnectId && ($iRow <= $this->iNumRows) )
{
if( mysql_data_seek( $this->iResultId , iRow ) )
{ $this->arRow = mysql_fetch_array( $this->iResultId );
$this->iCurrentRow = $iRow;
return true;
}
else
return false;
}
else
return false;
}
/***
Function: GetField
**************
Parameters:
$szFeldName == Ups, some German slipped in here.
The field name for which to retrieve the Data.
Return:
The Vaule of the Field.
What it's for:
Retrieve the Vaule for Field in the current Row.
*/
function GetField( $szFeldName )
{ return $this->arRow[$szFeldName];
}
/**
Function: EchoFeld
**************
Parameters:
$szFeldName == Ups, some German slipped in here.
The field name for which to retrieve the Data.
Return:
None.
What it's for:
Echo the Vaule for Field in the current Row.
*/
function EchoFeld( $szFeldName )
{
echo $this->arRow[$szFeldName];
}
/**
Function: NextRow
**************
Parameters:
None
Return:
true if all OK.
If something went wrong it will return false.
What it's for:
Move to the NextRow in the result Set.
This Function has to called directly after a query has been executed, otherwise
no Data is available for retrieval.
*/
function NextRow()
{
$iRet = true;
if( $this->iNumRows )
{ $this->arRow = mysql_fetch_array( $this->iResultId );
if( $this->arRow )
$this->iCurrentRow++;
else
$iRet = false;
}
else
{
$iRet = false;
}
return $iRet;
}
/**
Function: Close
**************
Parameters:
None
Return:
true on OK else false.
What it's for:
Close a Databse Connection.
Carefull here, if use initialised the Class with the function ReUSeConn.
All query on this database connection will fail. This also counts for
other instanceses of this Class.
*/
function Close()
{
return mysql_close( $this->iConnectId );
}
}
$database_inc_php3 = 1;
?>