Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class db {
- private $db_host = '';
- private $db_name = '';
- private $db_user = '';
- private $db_passw = '';
- public $connection;
- public $error;
- public function __construct(){
- $this->get_config();
- $this->connect();
- if(empty($this->error)) {} else {
- die($this->error);
- }
- }
- public function connect(){
- if($connection = mysql_connect($this->db_host, $this->db_user, $this->db_passw)){
- if(mysql_select_db($this->db_name, $connection)){
- $this->connection = $connection;
- return true;
- } else {
- $this->error = mysql_error();
- return false;
- }
- } else {
- $this->error = mysql_error();
- return false;
- }
- }
- public function select($sql){
- if($this->connection){
- mysql_set_charset('utf8', $this->connection);
- if(isset($sql) && $sql != ''){
- if($result = mysql_query($sql)){
- return $result;
- } else {
- $this->error = mysql_error();
- return false;
- }
- } else {
- $this->error = 'Błąd zapytania SQL';
- return false;
- }
- } else {
- $this->error = 'Brak połączenia z bazą danych';
- return false;
- }
- }
- public function query($sql){
- if(isset($sql) && $sql != ''){
- if($this->connection){
- mysql_set_charset('utf8', $this->connection);
- if(mysql_query($sql)){
- return true;
- } else {
- $this->error = mysql_error();
- return false;
- }
- } else {
- $this->error = 'Brak połączenia z bazą danych';
- return false;
- }
- } else {
- $this->error = 'Błąd zapytania SQL';
- return false;
- }
- }
- public function close(){
- if($this->connection){
- if (mysql_close($this->connection)){
- return true;
- } else {
- $this->error = mysql_error();
- return false;
- }
- } else {
- $this->error = 'Brak aktywnego połączenia';
- return false;
- }
- }
- public function secure($data) {
- if($this->connection){
- return mysql_real_escape_string(stripslashes($data));
- } else {
- $this->error = 'Brak aktywnego połączenia';
- return false;
- }
- }
- public function fetch($result, $mode = ''){
- switch ($mode){
- case 'ROW':
- return mysql_fetch_row($result);
- break;
- case 'ASSOC':
- return mysql_fetch_assoc($result);
- break;
- case 'OBJECT':
- return mysql_fetch_object($result);
- break;
- default:
- return mysql_fetch_array($result);
- break;
- }
- }
- public function get_config(){
- $db = require_once('configs/mysql.php');
- $this->db_host = $db['host'];
- $this->db_user = $db['username'];
- $this->db_passw = $db['password'];
- $this->db_name = $db['name'];
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment