Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database {
- // Database Connection Properties
- protected $host;
- protected $dbname;
- protected $username;
- protected $password;
- protected $dbConnectionStatus;
- public function __construct($host, $dbname, $username, $password)
- {
- $this->host = $host;
- $this->dbname = $dbname;
- $this->username = $username;
- $this->password = $password;
- }
- // Setup a Connection to the Database
- protected function setupDBConnection()
- {
- try
- {
- $conn = new PDO("mysql:host=$this->host;dbname=$this->dbname;charset=UTF8", $this->username, $this->password);
- $this->dbConnectionStatus = "connected";
- }
- catch (PDOException $e)
- {
- echo "Cannot Connect to Database (" . $e->getMessage() . ")";
- $this->dbConnectionStatus = "</br>" . "not connected";
- }
- }
- // Connect to the Database
- public function connectToDatabase()
- {
- $this->setupDBConnection();
- }
- // Output the status of the database connection
- public function getConnectionStatus()
- {
- echo "<h2>" . $this->dbConnectionStatus . "</h2>";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement