• My PHP Pages

    This is a complete and free PHP programming course for beginners. Everything you need to get started is set out in section one below.

Sep 18, 2013

Posted by Unknown
1 comment | 3:05 AM

 For this you basically need two files one is class file and other one is the file that where you calling PDO connection. You can reduce rewriting connection using this OOP PDO script. This class returns after the execution. if connection established successfully it returns '1' else it return error description

 Here is the Class.


dbuser = $dbuser;
 $this->dbpass = $dbpass;
 $this->dbhost = $dbhost;
 $this->dbname = $dbname;
  
       
   }
   function connect()
   {
 try
 {
 $DBH = '';
 $DBH = new PDO("mysql:host=$this->dbhost;dbname=$this->dbname", $this->dbuser, $this->dbpass);
 return 1;
 }
 catch(PDOexception $e)
 {
 return $e->getMessage();
 
 }
 }
}
// how to connect 
//$obj = new db_handler("Username","Password","Host","Database");
//$obj->connect();
?>
Use following code where you need to place connection
connect();
if($db == 1)
{
 echo 'connected';
 }
 else
 {
  echo $db;
  }
?>

1 comment :