Home > Mysql, PHP > Database Class in PHP5

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
 
?>

Regular Expression in MySQL

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.

Your email:

 


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:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: Mysql, PHP Tags: ,
  1. foo bar
    March 6th, 2008 at 23:39 | #1

    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…

  2. sycofly
    April 25th, 2008 at 04:35 | #2

    can you show how your would use “mysqli” class?

  3. Evan
    June 2nd, 2009 at 23:18 | #3

    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.

  4. October 5th, 2009 at 06:28 | #4

    I already use this class and it works perfectly bravoo

  5. Rick
    January 11th, 2010 at 03:36 | #5

    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

  6. April 4th, 2010 at 00:30 | #6

    This is an excellent article on use of php5.

  1. No trackbacks yet.