Connect MySQL Database using PHP in OOP


Let me assume that you have some background of PHP and MySQL based development . Now, you want to take advantage of object-oriented PHP to communicate with the database. Although, PHP provides, PDO class to provide some data abstraction, but we create our own, for some understanding about the language.
You simply create a file called “database.php” in your web-root directory, and using any of your favorite plain-text editor, copy and paste the following code. Then, execute the file: http://localhost:port/project_folder/database.php in your favorite browser, needless to say!

class Database{ //properties private $host; private $user; private $pwd; private $db; /////////////////////////////////// 
public $conxn; 
public $sql; 
public $res; 
public $affRows;
 public $numRows;
 public $data = array(); 
public $error; 
public $row;
/*public function Database(){ } */ 
public function __construct($host = 'localhost', 
                               $user = 'root',
                             $pwd = '',
                              $db = 'test'){ 
//connect to the server
$this->conxn = mysqli_connect($host, $user, $pwd, $db) 
		or trigger_error(mysqli_error($this->conxn));
//echo " The connection has been established" . $this->conxn->thread_id;
public function setHost($host){
$this->host = $host;	
public function setUser($user){
$this->user = $user;	
public function setPwd($pwd){
$this->pwd = $pwd;	
public function setDb($db){
$this->db = $db;	
public function getHost(){
return $this->host;	
public function getUser(){
return $this->user;	
public function getPwd(){
return $this->pwd;	
public function getDb(){
return $this->db;	
//destructor: it gets called automatically when an object of the class 
//is no further required, it does not need any args
public function __destruct(){
	//close the connection
	//echo " The connection has been closed" . $this->conxn->thread_id;
	@mysqli_close($this->conxn); // @ sign is used to suppress any error or warnings if any

//execute the selectQuery
public function selectQuery(){
//execute the sql
$this->res=mysqli_query($this->conxn, $this->sql) or
	//decisive parameter
	$this->numRows = mysqli_num_rows($this->res);
	if($this->numRows > 0){
	//there are records
	while($this->row = mysqli_fetch_assoc($this->res)){
		//put everything at the bottom : queue implementation
		array_push($this->data, $this->row);
		//$this->data[] = $this->row; //is same as above
	}//loop ends
	}//if ends
	//$this->data contains some information or completely blank
	return $this->data;
//method ends

public function nonSelectQuery(){
	//execute the sql
	$this->res = mysqli_query($this->conxn, $this->sql) or 
			error_log($this->error = mysqli_error($this->conxn));
//decisive parameter
$this->affRows = mysqli_affected_rows($this->conxn); // integer
if($this->affRows > 0){
return TRUE;	
return FALSE;	
}//method ends
}//class ends

//lets instantiate: create an object
$objDatabase = new Database();

//lets view the status og the object
echo '
echo '</pre>


The code explanation will follow shortly. In case, you have any question, please provide them in the comment box below.

About Dhiraj 39 Articles

Blogger on development, securities, and efficiency on web technologies.

1 Comment

Leave a Reply