<?php
require_once("class.db.cnx.php");
require_once("class.db.result.mysql.php");
/*
-------------------------------------------------------------------------
dbMysql Class
A mysql wrapper class to be used to handle mysql connectivity tasks.
-------------------------------------------------------------------------
Developer
Name -- Haddad Said.
Date -- 10-08-2005
Version -- 1.2
-------------------------------------------------------------------------
Member Functions
1.Constructor -- Obvious!
2.initialize -- Here we will do all the database server connectivity.
3.doQuery: -- Takes a mysql query and executes it.
4.cleanup -- cleans up the garbarge (destructor type)
-------------------------------------------------------------------------
*/
class dbCnxMysql extends dbCnx
{
//Variables to hold mysql database connectivity credentials
var $db_host;
var $db_user;
var $db_pass;
var $db_name;
/*
-------------------------------------------------------------------------
Class Constructor
Parameters:
host -- IP Address of the host running mysql service
user -- Username for logging in the mysql server
pass -- Password for the above user
name -- Name of the Database that we are going to use
Notes:
Sets up the initial values of the Mysql Database connectivity.
Example Usage:
$db = new dbCnxMysql("localhost", "user", "pass", "db_name")
-------------------------------------------------------------------------
*/
function dbCnxMysql($host, $user, $pass, $name)
{
$this->db_error = '';
$this->db_host = $host;
$this->db_user = $user;
$this->db_pass = $pass;
$this->db_name = $name;
}
/*
-------------------------------------------------------------------------
Method Initialize
Parameters:
Notes:
Returns a false when an error occurs and a true when db connection
and db selection is succesful. When an error occurs the error string
is stored in $this->db_error. You can use $this->sendError() to
display this error string.
Example Usage:
if($db->initialize())
{
//Execute some code on succesful connection here.
}
else
{
//Send the error encountered.
echo $db->sendError();
}
-------------------------------------------------------------------------
*/
function initialize()
{
if(!($this->db_cnx = @mysql_connect($this->db_host, $this->db_user, $this->db_pass)))
{
$this->db_error = "Could not Contact the MySQL Database Server " . mysql_error();
return false;
}
else if(!@mysql_select_db($this->db_name, $this->db_cnx))
{
$this->db_error = "Could not Connect to the Database " . $this->db_name ." " .mysql_error($this->db_cnx);
return false;
}
else
{
return true;
}
}
/*
-------------------------------------------------------------------------
Method doQuery
Takes a mysql query as a parameter and executes the query
Parameters:
query -- The query to be executed
Notes:
If the query is successful the method returns true and the result
resource is stored in $this->db_result. If the query is unsuccesful
a false is returned
Example Usage
$query = "Select user_id, user_name, user_email from tbluser";
if($db->doQuery($query))
{
//Execute some code to handle the resultset.
}
else
{
//Send the error
echo $db->getError();
}
-------------------------------------------------------------------------
*/
function doQuery($query)
{
if(!($this->db_result = mysql_query($query, $this->db_cnx)))
{
$this->db_error = "Could not perform the Query " . $query . " " . mysql_error($this->db_cnx);
return false;
}
else
{
return new dbResultMysql($this, $this->db_result);
}
}
/*
-------------------------------------------------------------------------
Method cleanup
This method cleans up after the object
Parameters:
Notes:
Not really necessary, included for completeness.
Example Usage:
//Lets do some cleaning
$db->cleanup();
-------------------------------------------------------------------------
*/
function cleanup()
{
mysql_free_result($this->db_result);
mysql_close($this->db_cnx);
$this->db_cnx = '';
}
}
?>