Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database{
- private $db;
- public function __construct($config){
- $driver = strtolower($config['database']['driver']);
- $host = $config['database']['host'];
- $database = $config['database']['name'];
- $username = $config['database']['username'];
- $password = $config['database']['password'];
- $this->connect($driver, $host, $database, $username, $password);
- }
- public function connect($driver, $host, $database, $username, $password){
- try{
- $this->db = new PDO($driver.":host=".$host.";dbname=".$database, $username, $password);
- $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }catch(PDOException $ex){
- $errorCode = $ex->getCode();
- $errorMessage = $ex->getMessage();
- $errorFile = $ex->getFile();
- $errorLine = $ex->getLine();
- include(dirname(dirname(__FILE__))."/error.php");
- }
- }
- public function nonQuery($query){
- try{
- $run = $this->db->prepare($query);
- if(func_get_args() > 0){
- $args = func_get_args();
- array_shift($args);
- return $run->execute($args);
- }else{
- return $run->execute();
- }
- }catch(PDOException $ex){
- $errorCode = $ex->getCode();
- $errorMessage = $ex->getMessage();
- $errorFile = $ex->getFile();
- $errorLine = $ex->getLine();
- include(dirname(dirname(__FILE__))."/error.php");
- }
- }
- function query($query){
- try{
- $run = $this->db->prepare($query);
- if(func_get_args() > 0){
- $args = func_get_args();
- array_shift($args);
- $run->execute($args);
- }else{
- $run->execute();
- }
- return $run; // using: ->fetchall(PDO::FETCH_ASSOC); needs: print_r($db->query("SELECT * FROM `test2`"));
- }catch(PDOException $ex){
- $errorCode = $ex->getCode();
- $errorMessage = $ex->getMessage();
- $errorFile = $ex->getFile();
- $errorLine = $ex->getLine();
- include(dirname(dirname(__FILE__))."/error.php");
- }
- }
- }
- $db = new Database($config);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement