Advertisement
Stratician

Database Class

Feb 3rd, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.89 KB | None | 0 0
  1. class Database {
  2.  
  3.  
  4.     // Database Connection Properties
  5.  
  6.     protected $host;
  7.     protected $dbname;
  8.     protected $username;
  9.     protected $password;
  10.     protected $dbStatus;
  11.  
  12.     public function __construct($host, $dbname, $username, $password)
  13.     {
  14.         $this->host = $host;
  15.         $this->dbname = $dbname;
  16.         $this->username = $username;
  17.         $this->password = $password;
  18.     }
  19.  
  20.     // Setup a Database Connection
  21.  
  22.     protected function dbConnection()
  23.     {
  24.         try
  25.         {
  26.             $conn = new PDO("mysql:host=$this->host;dbname=$this->dbname;charset=UTF8", $this->username, $this->password);
  27.             $this->dbStatus = "connected";
  28.         }
  29.  
  30.         catch (PDOException $e)
  31.         {
  32.             echo "Cannot Connect to Database (" . $e->getMessage() . ")";
  33.             $this->dbStatus = "</br>" . "not connected";
  34.         }
  35.     }
  36.  
  37.  
  38.     // Connect to the Database
  39.  
  40.     public function dbConnect()
  41.     {
  42.         $this->dbConnection();
  43.         echo "<h2>" . $this->dbStatus . "</h2>";
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement