You are on page 1of 8

A DATABASE CLASS FOR PHP APPLICATIONS.

Before Moving forward lets take quick look at the humble configuration file, You can find this file in the same directory as all other .class.php files,it looks likes this; <?php $dbHost $dbPort $dbUsername $dbPassword $dbDatabase ?>

= = = = =

'host_name'; '3306'; 'user_name'; 'password'; 'database_name';

this file is created during the code generation process detailed in the previous chapter.the content can be changed anytime,for custom configration.

The Database Class: Probably the most important class file in your model, it glues everythng together. The Error , Host and Database layers have all been implemented within the Database class. It has a number of methods and instance variables inside. Though you wont be using all of them because most methods are used by the class itself for your service. In this section we will get into the database classs anatomy.This section has been put here so you can understand whats going on inside db.class.php and can fiddle with the code yourself for customization if need be.

private $dbLink; private private private private private $dbHost; $dbPort; $dbUsername; $dbPassword; $dbDatabase;

private $error; private $errorNo; private $errorMsg; private private private private private $lastID; $result; $resultSet; $resultNumRows; $resultAffectedRows;

private $queryString; private $queryType;

As you can see the variables have been divided into five sections. private $dbLink; This holds the mysql_link object once the connection has been established.

private private private private private

$dbHost; $dbPort; $dbUsername; $dbPassword; $dbDatabase;

These are the host credentials used to acces database, holding Hostname, Port number, Username, password, and funally Database name.They are loaded through the configration file.when the class is instantiated. private $error; private $errorNo; private $errorMsg; The error variables used to handle exceptions, the $error is the flag used for condition programming throughout the db class, $errorNo is the numerical code of the error, and the $errorMsg is a string containing error description and nature. private private private private private $lastID; $result; $resultSet; $resultNumRows; $resultAffectedRows;

After running any query against the database you would want to get the result out and also require additional info. private $lastID; provides the last affected id after delete, insert, or update operation. private $result; A first single row from the whole result set. private $resultSet; The whole resultset returned after a Select query. private $resultNumRows; Number of rows returned only after select query.and null in other casses.

private $resultAffectedRows; Number of rows affected during an update or delete query.

private $queryString; private $queryType; Strictly used by the db class itself, $queryString contains the mySql query last ran through the object, and $queryType contains the type indicator of the query(i-e SELECT, DELETE, UPDATE, INSERT),this is used to set the specific result variables.

Now that you know their purposes,It is note worthy that all these are marked private and cannot be accessed directly, but through proper channel, that is each of them has their getter and setter method which can be used be used by the user to check for errors and for custom condition programming in their application. The getters and setters and have a particular format: GETTER METHOD: getDBLink() getDBHost() getDBPort() getDBUsername() getDBPassword() getDBDatabase() getError() getErrorNo() getErrorMsg() getLastID() getResult() getResultSet() getResultNumRows() getResultAffectedRows() getQueryString()

getQueryType() SETTER METHOD: setDBLink($s) setDBHost($s) setDBPort($s) setDBUsername($s) setDBPassword($s) setDBDatabase($s) setError($b) setErrorNo($n) setErrorMsg($s) setLastID($n) setResult($s) setResultSet($s) setResultNumRows($n) setResultAffectedRows($n) setQueryString($s) setQueryType($s) NOTE: here above the $s, $n and $b indicate the argument data type $s stands for a string/resource variable ,$b is for Boolean ,and $n is for numeric argument

THE PUBLIC METHODS: The Database layer of PACG does most of the work for you, in locating errors,or detecting query types,etc.But there are 3 methods that you can/may use yourself in certain situation. 1.Method: dbConnect() As the name suggests this method establishes a connection with the database.if you explicitly run this method,then in case where connection cannot be established,this loads the error variables mentioned above with the boolean flag TRUE , error number , and an error message- which can checked for programming and used to display fault nature back to user, otherwise it returns a true to indicate that connection has established. Whenever you run any query, the PACG checks database configration and builds the connection, but if you need to make another unique connection temporarily,you can use the Setters method-setDBDatabase($s)to change the DB name to run your query. Example Code: : : : : : : : : :

2.Method: dbQuery(): This method sets the $queryString variable with the Mysql query passed to it and executes the query using a private method execQuery(), which detects the query typ[e and an sets $queryType variables accordingly. Example; : : : : : : : 3.Method: dbDisconnect(): A simple method to safely close a MySql connection.

You might also like