SHARE
TWEET

Lean and Mean DB Class

Stratician Feb 4th, 2016 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class Database {
  4.  
  5.     // Database Connection Properties
  6.  
  7.     protected $host;
  8.     protected $dbname;
  9.     protected $username;
  10.     protected $password;
  11.     protected $connection;
  12.     protected $connectionStatus;
  13.  
  14.  
  15.     public function __construct($host, $dbname, $username, $password)
  16.     {
  17.         $this->host = $host;
  18.         $this->dbname = $dbname;
  19.         $this->username = $username;
  20.         $this->password = $password;
  21.     }
  22.  
  23.  
  24.     // Setup a Connection to the Database
  25.  
  26.     protected function setupConnection()
  27.     {
  28.         try
  29.         {
  30.             $connection = new PDO("mysql:host=$this->host;dbname=$this->dbname;charset=UTF8", $this->username, $this->password);
  31.             $this->connection = $connection;
  32.         }
  33.  
  34.         catch (PDOException $e)
  35.         {
  36.             echo "Cannot Connect to Database (" . $e->getMessage() . ")";
  37.         }
  38.     }
  39.  
  40.  
  41.     // Connect to the Database
  42.  
  43.     public function dbConnect()
  44.     {
  45.         $this->setupConnection();
  46.     }
  47.  
  48.  
  49.     // Get and output the status of the database connection
  50.  
  51.     public function getStatus()
  52.     {
  53.        
  54.         // Check the database connection status
  55.  
  56.         if($this->connection)
  57.         {
  58.             $this->connectionStatus = true;
  59.             var_dump($this->connectionStatus);
  60.         }
  61.  
  62.         else
  63.         {
  64.             $this->connectionStatus = false;
  65.             var_dump($this->connectionStatus);
  66.         }
  67.     }
  68.  
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top