Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $db = new Database();
- $db->setVariables();
- class Database{
- private $db;
- private $host;
- private $database;
- private $username;
- private $password;
- function setVariables(){
- global $config;
- $this->host = $config['server']['host'];
- $this->database = $config['server']['name'];
- $this->username = $config['master']['username'];
- $this->password = $config['master']['password'];
- $this->connect();
- }
- function connect(){
- try{
- $this->db = new PDO("mysql:host=1".$this->host.";dbname=".$this->database, $this->username, $this->password);
- $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }catch(PDOException $ex){
- $errorFile = file_get_contents(dirname(__FILE__)."/error.php");
- $errorFile = str_replace("%ErrorCode%", $ex->getCode(), $errorFile);
- $errorFile = str_replace("%ErrorMessage%", $ex->getMessage(), $errorFile);
- die($errorFile);
- }
- }
- function nonQuery($query){
- try{
- $run = $this->db->prepare($query);
- if(func_get_args() > 0){
- $args = func_get_args();
- array_shift($args);
- if($run->execute($args)){
- return true;
- }else{
- return false;
- }
- }else{
- if($run->execute()){
- return true;
- }else{
- return false;
- }
- }
- }catch(PDOException $ex){
- $errorFile = file_get_contents(dirname(__FILE__)."/error.php");
- $errorFile = str_replace("%ErrorCode%", $ex->getCode(), $errorFile);
- $errorFile = str_replace("%ErrorMessage%", $ex->getMessage(), $errorFile);
- die($errorFile);
- }
- }
- function query($query){
- try{
- $run = $this->db->prepare($query);
- if(func_get_args > 0){
- $args = func_get_args();
- $args = array_shift($args);
- $run->execute($args);
- }else{
- $run->execute();
- }
- return $run->fetchall(PDO::FETC_ASSOC);
- }catch(PDOException $ex){
- $errorFile = file_get_contents(dirname(__FILE__)."/error.php");
- $errorFile = str_replace("%ErrorCode%", $ex->getCode(), $errorFile);
- $errorFile = str_replace("%ErrorMessage%", $ex->getMessage(), $errorFile);
- die($errorFile);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement