Guest User

Untitled

a guest
Aug 27th, 2018
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | None | 0 0
  1. <?php
  2. /*****************
  3.  *
  4.  * Connect to MySQL
  5.  * Author: codekK
  6.  *
  7. *****************/
  8. class DBconfig {
  9.     // Define data connection
  10.     private $conn = null;
  11.     private static $server = "server"; // Server MySQL
  12.     private static $user = "user"; // Username MySQL
  13.     private static $pass = "pass"; // Password MySQL
  14.     private static $db = "db"; // DB MySQL
  15.    
  16.     public function conn(){
  17.         // Setting up connection
  18.         if(!isset($this->conn)){
  19.             try {
  20.                 $this->conn = new PDO("mysql:dbname=".self::$db.";host=".self::$server,self::$user,self::$pass);
  21.                 $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  22.         // Error
  23.             } catch (PDOException $e){
  24.                 echo 'Connection failed: ' . $e->getMessage();
  25.                 return false;
  26.             }  
  27.         }
  28.         return $this->conn;
  29.     }
  30.     public function close(){
  31.         unset($this->conn);
  32.     }
  33. }
  34. ?>
Add Comment
Please, Sign In to add comment