Guest User

Database

a guest
Jun 7th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. <?php
  2.  
  3. class Database {
  4. private static $db = "dbName";
  5. private static $host = "host";
  6. private static $username = "";
  7. private static $password = "";
  8.  
  9. private static $conn = null;
  10.  
  11. public function __construct(){
  12. die("Initialization is not allowed");
  13. }
  14.  
  15. public static function connect(){
  16. if(null == self::$conn){
  17. try {
  18. self::$conn = new PDO("mysql:host=".self::$host.";"."dbname=".self::$db, self::$username, self::$password);
  19. self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  20. } catch(PDOException $e){
  21. echo "Connection failed " . $e->getMessage();
  22. }
  23. }
  24. return self::$conn;
  25. }
  26.  
  27. public static function disconnect(){
  28. self::$conn = null;
  29. }
  30. }
Add Comment
Please, Sign In to add comment