Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Database {
- private $db_host = "";
- private $db_user = "";
- private $db_pass = "";
- private $db_name = null;
- private $DBH = null;
- private $select = null;
- private $insert = null;
- private $update = null;
- private $error;
- public function __construct($db_name, $db_host, $db_user, $db_pass, ErrorLog $error){
- $this->db_name = $db_name;
- $this->db_host = $db_host;
- $this->db_user = $db_user;
- $this->db_pass = $db_pass;
- $this->error = $error;
- $this->connect();
- }
- public function connect(){
- try {
- $this->DBH = new PDO('mysql:host=' . $this->db_host . ';dbname=' . $this->db_name . ';port=3306', $this->db_user, $this->db_pass);
- $this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch(PDOException $e) {
- error_log("Error connecting to database with message: " . $e->getMessage(), 0);
- }
- }
- public function disconnect(){
- $this->DBH = null;
- }
- public function select($db_query){
- $this->select = $this->DBH->query($db_query);
- $this->select->setFetchMode(PDO::FETCH_ASSOC);
- return json_encode($this->getRows());
- }
- public function selectWithParams($db_query, $params){
- $this->select = $this->DBH->prepare($db_query);
- for($i = 1; $i < sizeof($params) + 1; $i++){
- $this->select->bindParam($i, $params[$i - 1]);
- }
- if($this->select->execute()){
- return json_encode($this->getRows());
- }
- }
- public function getRows(){
- $return = $this->select->fetchAll(PDO::FETCH_ASSOC);
- $this->select = null;
- return $return;
- }
- public function insert($db_query, $params){
- $this->insert = $this->DBH->prepare($db_query);
- for($i = 1; $i < sizeof($params) + 1; $i++){
- $this->insert->bindParam($i, $params[$i - 1]);
- }
- if($this->insert->execute()){
- return true;
- } else {
- return false;
- }
- }
- public function insertGetID($db_query, $params){
- $this->insert = $this->DBH->prepare($db_query);
- for($i = 1; $i < sizeof($params) + 1; $i++){
- $this->insert->bindParam($i, $params[$i - 1]);
- }
- if($this->insert->execute()) {
- return $this->DBH->lastInsertId();
- }
- }
- public function update($db_query, $params){
- $this->update = $this->DBH->prepare($db_query);
- for($i = 1; $i < sizeof($params) + 1; $i++){
- $this->update->bindParam($i, $params[$i - 1]);
- }
- if($this->update->execute()){
- $this->update = null;
- return true;
- } else {
- return false;
- }
- }
- public function delete($db_query){
- if($this->DBH->query($db_query)){
- return true;
- } else {
- return false;
- }
- }
- public function interpolateQuery($query, $params) {
- $keys = array();
- foreach ($params as $key => $value) {
- if (is_string($key)) {
- $keys[] = '/:'.$key.'/';
- } else {
- $keys[] = '/[?]/';
- }
- }
- $query = preg_replace($keys, $params, $query, 1, $count);
- error_log($query, 0);
- }
- public function setHost($host){
- $this->db_host = $host;
- }
- public function setUser($user){
- $this->db_user = $user;
- }
- public function setPass($pass){
- $this->db_pass = $pass;
- }
- public function setName($name){
- $this->db_name = $name;
- }
- public function setAll($host, $user, $pass, $name){
- $this->db_host = $host;
- $this->db_user = $user;
- $this->db_pass = $pass;
- $this->db_name = $name;
- }
- public function getError(){
- return $this->DBH->errorInfo();
- }
- }
Add Comment
Please, Sign In to add comment