Advertisement
Stratician

DB Class

Feb 4th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  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 $dbConnectionStatus;
  12.  
  13.  
  14.     public function __construct($host, $dbname, $username, $password)
  15.     {
  16.         $this->host = $host;
  17.         $this->dbname = $dbname;
  18.         $this->username = $username;
  19.         $this->password = $password;
  20.     }
  21.  
  22.  
  23.     // Setup a Connection to the Database
  24.  
  25.     protected function setupDBConnection()
  26.     {
  27.         try
  28.         {
  29.             $conn = new PDO("mysql:host=$this->host;dbname=$this->dbname;charset=UTF8", $this->username, $this->password);
  30.             $this->dbConnectionStatus = "connected";
  31.         }
  32.  
  33.         catch (PDOException $e)
  34.         {
  35.             echo "Cannot Connect to Database (" . $e->getMessage() . ")";
  36.             $this->dbConnectionStatus = "</br>" . "not connected";
  37.         }
  38.     }
  39.  
  40.  
  41.     // Connect to the Database
  42.  
  43.     public function connectToDatabase()
  44.     {
  45.         $this->setupDBConnection();
  46.     }
  47.  
  48.  
  49.     // Output the status of the database connection
  50.  
  51.     public function getConnectionStatus()
  52.     {
  53.         echo "<h2>" . $this->dbConnectionStatus . "</h2>";
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement