PHP Mysql Connection by OOPHP pattern

Hello Friends.  Hope you are fine. Now, I am going to show, how to connect PHP and MySql by using Object Oriented PHP pattern. First of all, creating a new php file by using dreamweaver. Then I am calling a class called database. Then declare the necessary variables. Then Code are below-

class database{
                          //defining the variables
                          public $db_host;
                          public $db_user;
                          public $db_pass;
                          public $db_database;
}

Now, I am creating the functions. First of all, I am creating the function for database connection called function db_connect(). Then I am declaring the necessary functions. The code are below-

function db_connect(){
                         $connect = @mysql_connect($this->db_host, $this->db_user, $this->db_pass);
if(!$connect){
                         @mysql_close($connect);
                         die(‘<h1><font color=”#ff0″>Error! MySql Connection Failed!</font></h1>’);
                         return flase;
}
                         return true;
}

Now, I am creating another necessary function for choosing the select database. The Code are in below-

function db_select(){
                         $select = @mysql_select_db($this->db_database);
 if(!$select){
                         @mysql_close($connect);
                         die (“<h1><font color=’#000000′><center>Not <font color=’#FF0000’style=’text-decoration:blink;’>Selected</font> DataBase !</center></font></h1>”);
                         return false;
}
                         return true;
}

Now, the time for calling the class and objects. The Total files are in below-

<?php
//creating the class
class database{
 
 //defining the vairables
 public $db_host;
 public $db_user;
 public $db_pass;
 public $db_database;
 
 
 //writing the connection function
 function db_connect(){
 
 $connect = @mysql_connect($this->db_host, $this->db_user, $this->db_pass); 
 
 if(!$connect){
 @mysql_close($connect);
 die('<h1><font color="#ff0">Error! MySql Connection Failed!</font></h1>');
 return flase;
 }
 return true;
 } 
 
 //writing the sselect function for database
 function db_select(){
 $select = @mysql_select_db($this->db_database);
 
 if(!$select){
 @mysql_close($connect);
 die ("<h1><font color='#000000'><center>Not <font color='#FF0000'style='text-decoration:blink;'>Selected</font> DataBase !</center></font></h1>"); 
 return false;
 } 
 return true;
 }
 
}//class database{

//call the class
$db = new database();
$db->db_host = 'localhost';
$db->db_user = 'root';
$db->db_pass= '';
$db->db_database = 'test';
//calling the class
$db->db_connect();
$db->db_select();

?>

 

That’s enough for today. Wait for next tutorial. Keep in touch.

bye

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s