Created
February 8, 2012 18:26
-
-
Save sbarrat/1771941 to your computer and use it in GitHub Desktop.
Singleton PDO DB Connection PHP
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
final class DBConnection { | |
private static $_handle = null; | |
private static $_dsn = "mysql:dbname=mydb;host=my.host.com;port=3306"; | |
private static $_user = "myUserName"; | |
private static $_password = "myPassword"; | |
private static $_options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"); // SET UTF-8 | |
/** | |
* Deny Construct | |
*/ | |
private function __construct() | |
{ | |
} | |
/** | |
* Deny Clone | |
*/ | |
private function __clone() | |
{ | |
} | |
/** | |
* Function to connect width database | |
*/ | |
function connect() | |
{ | |
if ( is_null( self::$_handle ) ) { | |
try { | |
self::$_handle = new PDO( | |
self::$_dsn, self::$_user, self::$_password, self::$_options | |
); | |
} catch ( PDOException $error ){ | |
die ( $error->getMessage() ); | |
} | |
} | |
return self::$_handle; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment