Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('HOST','localhost');
- define('USER','root');
- define('PASS','');
- define('DB','prodavnica');
- /*define('HOST','sql212.byethost18.com');
- define('USER','b18_16679881');
- define('PASS','gamemaker');
- define('DB','b18_16679881_prodavnica');*/
- class Database {
- private $_localhost;
- private $_username;
- private $_password;
- private $_database;
- private $_konekcija;
- function __construct($host,$user,$pass,$db) {
- $this->_localhost = $host;
- $this->_username = $user;
- $this->_password = $pass;
- $this->_database = $db;
- $this->_konekcija = $this->connect(HOST,USER,PASS,DB);
- }
- public function connect() {
- @$connection = new mysqli($this->_localhost,$this->_username,$this->_password,$this->_database);
- if($connection->connect_errno) {
- die('Ne mogu da se povezem sa bazom podataka');
- }
- return $connection;
- }
- public function escape_string($string) {
- return mysqli_real_escape_string($this->_konekcija,$string);
- }
- public function select($query) {
- $connection = $this->_konekcija;
- try{
- if(!@$res = $connection->query($query)) throw new Exception('Greska.');
- else if(!@$res->num_rows) return false;
- $row = $res->fetch_all(MYSQLI_ASSOC);
- return $row;
- } catch (Exception $ex) {
- return false;
- }
- }
- public function update($query) {
- $connection = $this->_konekcija;
- try {
- if($connection->query($query)) return true;
- else throw new Exception('Greska.');
- } catch(Exception $ex) {
- return false;
- }
- }
- public function insert($query) {
- $connection = $this->_konekcija;
- try{
- if(!$res = $connection->query($query)) throw new Exception('Trenutno ne mogu da saljem nista.');
- else return $connection->insert_id;
- } catch (Exception $ex) {
- echo 'Greska: '.$ex->getMessage();
- }
- }
- public function __destruct()
- {
- $this->_konekcija->close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement