Created
March 23, 2015 20:40
-
-
Save tdharris/f599ae785a793b047445 to your computer and use it in GitHub Desktop.
phpDatabase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Database { | |
private $host = "showcase.tdharris.net"; | |
private $user = "tdharris_admin"; | |
private $pass = "mylittlesecret"; | |
private $dbname = "tdharris_showcase"; | |
private $dbh; | |
private $error; | |
private $stmt; | |
public function __construct() { | |
$dsn = 'mysql:host=' . $this -> host . ';dbname=' . $this-> dbname . ';charset=utf8'; | |
$options = array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"); | |
try { | |
$this -> dbh = new PDO($dsn, $this -> user, $this -> pass, $options); | |
} catch (PDOException $e) { | |
// $this -> error = $e -> getMessage(); | |
$error_message = $e->getMessage(); | |
include 'error.php'; | |
exit; | |
} | |
try { | |
echo $this->dsn; | |
$this->dbh = new PDO('mysql:host=' . $host . ';dbname=' . $dbname, $this->username, $this->password, $this->options); | |
/*** echo a message saying we have connected ***/ | |
echo 'Connected to database'; // Test with this string | |
} | |
catch(PDOException $e) { | |
$error_message = $e->getMessage(); | |
include 'error.php'; | |
exit; | |
} | |
} | |
public function __destruct() { | |
$this->dbh = NULL; // Setting the handler to NULL closes the connection propperly | |
} | |
} | |
$db = new Database; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment