Database Class in PHP5
In PHP5 due to Object Oriented Concepts, the database connection to MySql will be using Object Oriented concept. This tutorial will guide you creating an database class that will connect to Mysql database and retrieve records from them. This tutorial does not cover exception handling in case of any database connection error as will have to write an Exception Handling class to handle database error.
< ? class database { private $connectlink; //Database Connection Link private $username = "--database username--"; private $password = "--database password--"; private $database = "--database name--"; private $hostname = "localhost"; private $resultlink; //Database Result Recordset link private $rows; //Stores the rows for the resultset public function __construct() { $this->connectlink = mysql_connect($this->hostname,$this->username,$this->password); if(!($this->connectlink)) { //throw new DatabaseConnectionException("Error Connecting to the Database".mysql_error(),"101"); } else { mysql_select_db($this->database); } } public function __destruct() { @mysql_close($this->connectlink); } public function query($sql) { $this->resultlink = mysql_query($sql); return $this->resultlink; } public function fetch_rows($result) { $rows = array(); if($result) { while($row = mysql_fetch_array($result)) { $rows[] = $row; } } else { //throw new RetrieveRecordsException("Error Retrieving Records".mysql_error(),"102"); $rows = null; } return $rows; } } $db = new database(); //Create database object ?>
NOTE:
Here i am connecting to MySql database but if we want to connect to other database provider then will have to change the code wherever mysql function code is written.
PHP Code Explanation:
- Here i have declared 7 properties which stores all the important parameter related to the Mysql database connection
- __construct() function is responsible for creating Mysql database connection. It also connects to the database once the mysql connection is successful. To know more about __construct function check this link __construct method in PHP5
- __destruct() will close all the database connection created during object creation. To know more about __destruct function check this link __destruct method in PHP5
- query() member function is responsible for executing the sql query. This member function accepts the sql query. This function will return the result link for the query executed
- fetch_rows() member function will fetch all the rows retrieved from executing the sql query. All the rows values will be stored in $rows array
- Finally i am creating the object of database class
Custom Search
Popular Articles:
- Column count doesn’t match value count in MySql
- OOPS in PHP5 – __construct() Method
- How to Implement Text to Speech in PHP
- OOPS in PHP5 – Define Methods for the Class
- OOPS in PHP 5 – __destruct() Method
- OOPS in PHP 5 Tutorial – Exploring Inheritance
- PHP5 Tutorial – __autoload Magic Method
- OOPS in PHP 5 Tutorial – Abstract Class
- OOPS in PHP 5 Tutorial – Parent Keyword
- MySql Batch Insert Using PHP



































Almost!
The ‘properties’ are per-instance state values, and the ‘object’ has no good way to set them.
They should be settable when constructing, not hard-coded.
Also, they should NOT be settable after constructing.
However, they could be gettable…
can you show how your would use “mysqli” class?
Um…ok, so how do you actually use it? I tested the code but I don’t understand how to use this for a select statement where I would want to output the rows.
I already use this class and it works perfectly bravoo
Thanks, I love the simplicity of the class.
I modified the fetch_rows class to do the query for me so now all I have to do is run once command like:
$db->fetch_rows(“SELECT * FROM categories”);
———
public function fetch_rows($sql) {
$result = $this->query($sql);
$rows = array();
if($result)
{
while($row = mysql_fetch_array($result))
{
$rows[] = $row;
}
}
else {
//throw new RetrieveRecordsException(“Error Retrieving Records”.mysql_error(),”102″);
$rows = null;
}
return $rows;
}
—-
Hope this helps!
Rick
This is an excellent article on use of php5.