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; } ?>
kütahya
ReplyDeletetunceli
ardahan
düzce
siirt
21SWH