Connect MySQL Database using PHP in OOP

Share

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!

 <?php 
//database.php 
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;
 ///////////////////////////////////////////////////// 
//constructor 
/////////////////////////////////////////////////// 
/*public function Database(){ } */ 
public function __construct($host = 'localhost', 
                               $user = 'root',
                             $pwd = '',
                              $db = 'test'){ 
//connect to the server
 $this->setHost($host);
$this->setUser($user);
$this->setPwd($pwd);
$this->setDb($db);
$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;
	
}
//setters
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;	
}
//getters
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
error_log($this->error=mysqli_error($this->conxn).$_SERVER['SCRIPT_NAME']);
	//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

////////////////////////////////////////////////////////
//nonSelectQuery
//////////////////////////////////////////////////////
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){
//success
return TRUE;	
}else{
//failure
return FALSE;	
}
}//method ends
}//class ends

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

//lets view the status og the object
echo '
<pre>';
print_r($objDatabase);
echo '</pre>

';

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

About Dhiraj 40 Articles
Blogger on development, securities, and efficiency on web technologies.

1 Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.