Last active
September 1, 2015 19:56
-
-
Save henriquehorbovyi/b6984d8302eb558ac68c to your computer and use it in GitHub Desktop.
Database Connection PHP using the class PDO.
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 | |
| class Connection{ | |
| public static $db; | |
| private static function doConnection(){ | |
| if (!isset(self::$db)) { | |
| try { | |
| $host = "YOUR_HOST"; | |
| $db_name = "YOUR_DB"; | |
| $user = "YOUR_USER"; | |
| $pass = "YOUR_PASS"; | |
| self::$db = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass); | |
| self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
| self::$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); | |
| } | |
| catch (PDOException $e) { | |
| echo "CONNECTION ERRO" . $e->getMessage(); | |
| } | |
| } | |
| return self::$db; | |
| } | |
| public static function prepare($sql){ | |
| return self::doConnection()->prepare($sql); | |
| } | |
| } |
Author
VLW
Author
I indentation fixed!!! Thanks!! 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a pretty version: https://gist.github.com/adorilson/131091bc3c83c0af49e6 ;)
Yes, correct indentation matter.