Created
May 27, 2013 13:01
-
-
Save wouterds/5656947 to your computer and use it in GitHub Desktop.
My perfect PDO connection
This file contains hidden or 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 | |
namespace Travellar\Service; | |
use PDO; | |
use PDOException; | |
use Exception; | |
class ConnectionService { | |
public static function getInstance() { | |
try { | |
$dbh = ConfigurationService::DB_TYPE . ':host=' . ConfigurationService::DB_HOST . ';dbname=' . ConfigurationService::DB_NAME; | |
$dbh = new PDO($dbh, ConfigurationService::DB_USER, ConfigurationService::DB_PASS); | |
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); | |
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$dbh->exec("SET NAMES utf8"); | |
return $dbh; | |
} | |
catch(PDOException $e) { | |
throw new Exception($e->getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment