PHP Classes

File: classes/db_mysql.class.php

Recommend this page to a friend!
  Packages of ZLioxygon   Minimalistic DB   classes/db_mysql.class.php   Download  
File: classes/db_mysql.class.php
Role: Class source
Content type: text/plain
Description: Extended class
Class: Minimalistic DB
Database abstraction layer with minimal interface
Author: By
Last change:
Date: 13 years ago
Size: 3,071 bytes
 

 

Contents

Class file image Download
<?php
/******************************************************************************
MySQL
/******************************************************************************/
final class db_mysql extends db{
/******************************************************************************/
    public function __construct($config){
        $this->link = @mysqli_connect(
            $config['host'],
            $config['user'],
            $config['pass'],
            $config['base'],
            $config['port'],
            $config['sock']
        );
        if(!$this->link){
            throw new db_Exeption('MySQL #'.@mysqli_connect_errno($this->link).': '.@mysqli_connect_error($this->link).'!', 1);
        }
        if($config['coll'] !== false){
            self::c($config['coll']);
        }
    }
/******************************************************************************/
    public function __destruct(){
        if(!!$this->link){
            @mysqli_close($this->link);
        }
    }
/******************************************************************************/
    public function q($query){
        $timer = microtime(true);
        $this->rows = 0;
        $this->fields = 0;
        $return = null;
        if(!is_string($query) or empty($query)){
            throw new db_Exeption('MySQL #2: Invalid query!', 2);
        }
        $result = mysqli_query($this->link, $query);
        $this->counter += 1;
        if($result === false){
            $this->timer += (microtime(true) - $timer);
            throw new db_Exeption('MySQL #'.mysqli_errno($this->link).': '.mysqli_error($this->link).'!', 3);
        }
        elseif($result === true){
            $this->rows = mysqli_affected_rows($this->link);
            $return = true;
        }
        else{
            $this->fields = mysqli_num_fields($result);
            $this->rows = mysqli_num_rows($result);
            $return = array();
            while(($row = mysqli_fetch_assoc($result)) !== null){
                $return[] = $row;
            }
            mysqli_free_result($result);
        }
        $this->timer += (microtime(true) - $timer);
        return($return);
    }
/******************************************************************************/
    public function s($data){
        if(is_array($data)){
            foreach($data as $key => $val){
                $data[$key] = self::s($val);
            }
            return($data);
        }
        if(function_exists('mysqli_real_escape_string')){
            $data = mysqli_real_escape_string($this->link, $data);
        }
        elseif(function_exists('mysqli_escape_string')){
            $data = mysqli_escape_string($this->link, $data);
        }
        else{
            $data = addslashes($data);
        }
        return($data);
    }
/******************************************************************************/
    public function c($collate){
        if(@mysqli_set_charset($this->link, $collate)){
            return(true);
        }
        else{
            throw new db_Exeption('MySQL #'.mysqli_errno($this->link).': '.mysqli_error($this->link).'!', 3);
        }
    }
/******************************************************************************/
    public function l(){
        return(($this->link !== false) ? @mysqli_insert_id($this->link) : false);
    }
/******************************************************************************/
}
/******************************************************************************/