Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database{
- private $hostdb = "localhost";
- private $namedb = "bd_name";
- private $userdb = "root";
- private $passdb = "";
- public $pdo;
- public function __construct(){
- if(!isset($this->pdo)){
- try{
- $link = new PDO("mysql:host=".$this->hostdb.";dbname=".$this->namedb, $this->userdb, $this->passdb);
- $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $link->exec("SET CHARACTER SET utf8");
- $this->pdo = $link;
- }
- catch (PDOException $rex){
- die("Failed to connect with database".$rex->getMessage());
- }
- }
- }
- }
- ?>
- **********************************************
- Using OOP const(constant)
- **********************************************
- <?php
- class Database {
- const HOST = "localhost";
- const USER = "root";
- const PASS = "";
- const DB = "bd_name";
- public $conn;
- public function __construct(){
- if (!isset($this->conn)) {
- try {
- $sql = new PDO("mysql:host=" . Database::HOST . ";dbname=" . Database::DB, Database::USER, Database::PASS);
- $sql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql->exec("SET CHARACTER SET utf8");
- $this->conn = $sql;
- } catch (PDOException $e) {
- die("Faied to connection with Database!".$e->getMessage());
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement