Skip to content

Instantly share code, notes, and snippets.

@JaniKibichi
Last active January 27, 2022 10:37
Show Gist options
  • Save JaniKibichi/486057ff7961a81f1a08fd9c8a87a2ec to your computer and use it in GitHub Desktop.
Save JaniKibichi/486057ff7961a81f1a08fd9c8a87a2ec to your computer and use it in GitHub Desktop.
PHP DB Connector
<?php
include_once './util.php';
class DBConnector {
var $pdo;
function __construct(){
$dsn= "mysql:host=". Util::$SERVER_NAME . ";dbname=" . Util::$DB_NAME ."";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
//Notice here that we read data from tables and is presented as an associative array
//indexed by names of column
PDO::ATTR_DEFAULT_FETCH_MODE =>PDO::FETCH_ASSOC
];
try{
$this->pdo = new PDO($dsn, Util::$DB_USER, Util::$DB_USER_PASS, $options);
}catch (PDOException $e){
echo $e->getMessage();
}
}
public function connectToDB(){
return $this->pdo;
}
public function closeDB(){
$this->pdo = null;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment