Skip to content

Instantly share code, notes, and snippets.

@wouterds
Created May 27, 2013 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wouterds/5656947 to your computer and use it in GitHub Desktop.
Save wouterds/5656947 to your computer and use it in GitHub Desktop.
My perfect PDO connection
<?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