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
Similar Posts
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


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…