Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.13 KB | None | 0 0
  1. <?php
  2. define('HOST','localhost');
  3. define('USER','root');
  4. define('PASS','');
  5. define('DB','prodavnica');
  6.  
  7. /*define('HOST','sql212.byethost18.com');
  8. define('USER','b18_16679881');
  9. define('PASS','gamemaker');
  10. define('DB','b18_16679881_prodavnica');*/
  11. class Database {
  12.     private $_localhost;
  13.     private $_username;
  14.     private $_password;
  15.     private $_database;
  16.     private $_konekcija;
  17.  
  18.     function __construct($host,$user,$pass,$db) {
  19.         $this->_localhost = $host;
  20.         $this->_username = $user;
  21.         $this->_password = $pass;
  22.         $this->_database = $db;
  23.         $this->_konekcija = $this->connect(HOST,USER,PASS,DB);
  24.     }
  25.     public function connect() {
  26.         @$connection =  new mysqli($this->_localhost,$this->_username,$this->_password,$this->_database);
  27.         if($connection->connect_errno) {
  28.             die('Ne mogu da se povezem sa bazom podataka');
  29.         }
  30.         return $connection;
  31.     }
  32.     public function escape_string($string) {
  33.         return mysqli_real_escape_string($this->_konekcija,$string);
  34.     }
  35.     public function select($query) {
  36.         $connection = $this->_konekcija;
  37.         try{
  38.             if(!@$res = $connection->query($query)) throw new Exception('Greska.');
  39.             else if(!@$res->num_rows) return false;
  40.  
  41.             $row = $res->fetch_all(MYSQLI_ASSOC);
  42.             return $row;
  43.         } catch (Exception $ex) {
  44.             return false;
  45.         }
  46.     }
  47.     public function update($query) {
  48.         $connection = $this->_konekcija;
  49.         try {
  50.             if($connection->query($query)) return true;
  51.             else throw new Exception('Greska.');
  52.         } catch(Exception $ex) {
  53.             return false;
  54.         }
  55.     }
  56.     public function insert($query) {
  57.         $connection = $this->_konekcija;
  58.         try{
  59.             if(!$res = $connection->query($query)) throw new Exception('Trenutno ne mogu da saljem nista.');
  60.             else return $connection->insert_id;
  61.     } catch (Exception $ex) {
  62.         echo 'Greska: '.$ex->getMessage();
  63.         }
  64.     }
  65.     public function __destruct()
  66.     {
  67.         $this->_konekcija->close();
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement