Created
March 25, 2017 15:20
-
-
Save Deviad/5548ca318cf30fd8c2be5728bca043d1 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace Db; | |
class DbMgmt | |
{ | |
const SERVERNAME = 'localhost'; | |
const PORT = "3306"; | |
const USERNAME = 'root'; | |
const PASSWORD = ''; | |
const DBNAME = 'universumphp'; | |
const CHARSET = 'utf8'; | |
public $conn; | |
private static $instance = false; | |
public function __constructor() { | |
$this->newConn(); | |
} | |
public function newConn() | |
{ | |
if (!self::$instance) { | |
$sql = 'USE ' . self::DBNAME; | |
try { | |
$this->conn = new \PDO('mysql:host=' . self::SERVERNAME . ";port=" . self::PORT . ";charset=" . self::CHARSET, self::USERNAME, self::PASSWORD); | |
$this->conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); | |
$this->conn->exec($sql); | |
// echo "Connection established <br>"; | |
self::$instance = true; | |
} catch (\PDOException $e) { | |
echo $sql . '<br>' . $e->getMessage(); | |
} | |
} else { | |
throw new \Exception('An instance of Connection is already present!'); | |
} | |
return $this->conn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment