Skip to content

Instantly share code, notes, and snippets.

@Deviad
Created March 25, 2017 15:20
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 Deviad/5548ca318cf30fd8c2be5728bca043d1 to your computer and use it in GitHub Desktop.
Save Deviad/5548ca318cf30fd8c2be5728bca043d1 to your computer and use it in GitHub Desktop.
<?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