Last active
January 27, 2022 10:37
-
-
Save JaniKibichi/486057ff7961a81f1a08fd9c8a87a2ec to your computer and use it in GitHub Desktop.
PHP DB Connector
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 | |
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