zaba141

Mysql.class.php

Apr 5th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2. class db {
  3.     private $db_host = '';
  4.     private $db_name = '';
  5.     private $db_user = '';
  6.     private $db_passw = '';
  7.  
  8.     public $connection;
  9.     public $error;
  10.  
  11.     public function __construct(){
  12.         $this->get_config();
  13.         $this->connect();
  14.         if(empty($this->error)) {} else {
  15.         die($this->error);
  16.         }
  17.     }
  18.  
  19.     public function connect(){
  20.         if($connection = mysql_connect($this->db_host, $this->db_user, $this->db_passw)){
  21.             if(mysql_select_db($this->db_name, $connection)){
  22.                 $this->connection = $connection;
  23.                 return true;
  24.             } else {
  25.                 $this->error = mysql_error();
  26.                 return false;
  27.             }
  28.         } else {
  29.             $this->error = mysql_error();
  30.             return false;
  31.         }
  32.     }
  33.  
  34.     public function select($sql){
  35.         if($this->connection){
  36.             mysql_set_charset('utf8', $this->connection);
  37.             if(isset($sql) && $sql != ''){
  38.                 if($result = mysql_query($sql)){
  39.                     return $result;
  40.                 } else {
  41.                     $this->error = mysql_error();
  42.                     return false;
  43.                 }
  44.             } else {
  45.                 $this->error = 'Błąd zapytania SQL';
  46.                 return false;
  47.             }
  48.         } else {
  49.             $this->error = 'Brak połączenia z bazą danych';
  50.             return false;
  51.         }
  52.   }
  53.  
  54.     public function query($sql){
  55.         if(isset($sql) && $sql != ''){
  56.             if($this->connection){
  57.                 mysql_set_charset('utf8', $this->connection);
  58.  
  59.                 if(mysql_query($sql)){
  60.                     return true;
  61.                 } else {
  62.                     $this->error = mysql_error();
  63.                     return false;
  64.                 }
  65.             } else {
  66.                 $this->error = 'Brak połączenia z bazą danych';
  67.                 return false;
  68.             }
  69.         } else {
  70.             $this->error = 'Błąd zapytania SQL';
  71.             return false;
  72.         }
  73.     }
  74.  
  75.     public function close(){
  76.         if($this->connection){
  77.             if (mysql_close($this->connection)){
  78.                 return true;
  79.             } else {
  80.                 $this->error = mysql_error();
  81.                 return false;
  82.             }
  83.         } else {
  84.             $this->error = 'Brak aktywnego połączenia';
  85.             return false;
  86.         }
  87.     }
  88.    
  89.         public function secure($data) {
  90.         if($this->connection){
  91.             return mysql_real_escape_string(stripslashes($data));
  92.         } else {
  93.             $this->error = 'Brak aktywnego połączenia';
  94.             return false;
  95.         }
  96.     }
  97.  
  98.     public function fetch($result, $mode = ''){
  99.         switch ($mode){
  100.             case 'ROW':
  101.                 return mysql_fetch_row($result);
  102.                 break;
  103.  
  104.             case 'ASSOC':
  105.                 return mysql_fetch_assoc($result);
  106.                 break;
  107.            
  108.             case 'OBJECT':
  109.                 return mysql_fetch_object($result);
  110.                 break;
  111.  
  112.             default:
  113.                 return mysql_fetch_array($result);
  114.                 break;
  115.         }
  116.     }
  117.  
  118.     public function get_config(){
  119.         $db = require_once('configs/mysql.php');
  120.         $this->db_host = $db['host'];
  121.         $this->db_user = $db['username'];
  122.         $this->db_passw = $db['password'];
  123.         $this->db_name = $db['name'];
  124.     }
  125.  
  126. }
  127.  
  128. ?>
Advertisement
Add Comment
Please, Sign In to add comment