PHP Classes
elePHPant
Icontem

File: pdodb_class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of ofertino  >  PdoDB  >  pdodb_class.php  >  Download  
File: pdodb_class.php
Role: Class source
Content type: text/plain
Description: MAIN CLASS
Class: PdoDB
Connect to databases and execute queries using PDO
Author: By
Last change: Removed deprecated function, added public and private(PHP5)
Added some explanation of the functions and fields.
Date: 3 years ago
Size: 2,737 bytes
 

Contents

Class file image Download
<?
/*
C OFERTINO 2012
*/
class PdoDB{
    private
$connect_array = array('DB0'=>'mysql:host=;dbname=,user,password');
    private
$selected_db;
    public
$set_names = false;
    public
$set_names_type = 'utf8';
    public
$debug = false;
    public
$get_time = false;
    public
$insert_delayed = false;
    public
$all_time = array();
    private static
$version = '1.0';
    public function
__construct($DB){ //Construct the class $DB - Id from $connect_array ex. DB0
       
$this->selected_db = $DB;
       
$this->create_list();
       
$this->connect();
    }
    private function
create_list(){ //Create list for PDO to connect
       
list($this->connecting, $this->connecting2, $this->connecting3)=explode(",",$this->connect_array[$this->selected_db]);
    }
    private function
connect(){ //Connect to MySQL database
       
if($this->get_time){
           
$start = microtime();
        }
       
$this->pdo = new PDO($this->connecting, $this->connecting2, $this->connecting3);
        @
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        if(
$this->set_names){
            @
$this->pdo->query('SET NAMES '.$this->set_names_type);
            @
$this->pdo->query('SET CHARACTER SET '.$this->set_names_type);
        }
        if(
$this->get_time){
           
$stop = microtime();
           
$time =($stop-$start)*1000;
           
$this->all_time['connect'] = $time;
        }
        if(
$this->debug){
            echo
"Script Version ".$this->version;
            echo
"Connecting ".$this->m_type."...<br>";
            echo
"Connecting from ".$_SERVER['SCRIPT_NAME']."...<br>";
            if(
$this->get_time){
                echo
"Connection time ".$time;
            }
        }
        if(
$this->pdo==true){
            return
true;
        }
        return
false;
    }
    public function
query($SQL){ // Query a given SQL, replace INSERT to INSERT DELAYED if $insert_daleyed is true
       
if($this->insert_delayed){
           
$sql=str_replace("INSERT","INSERT DELAYED",$sql);
        }
        if(
$this->get_time){
           
$start = microtime();
        }
       
$this->record= @$this->pdo->query($SQL);
        if(
$this->get_time){
           
$stop = microtime();
           
$time =($stop-$start)*1000;
           
$this->all_time['sql_query'] = $time;
        }
       
$this->rowcount=$this->record->rowCount();
        if(
$this->debug){
            echo
"Query->".$sql." || TIME : $time s<BR>";
        }
        return
$this->record;
    }
    public function
next_record() { //Fetches next record
       
$this->row=$this->record->fetch();
       
$this->row_number +=1;
    }
    public function
f($row){ //Return a given row ex. ID,name,etc.
       
return $this->row[$row] ;
    }
    public function
Eof($pos = 0) { //Used for loops, see in example file
       
$status= $this->rowcount-$this->row_number+1 ;
        if (
$status<=0){$status=0;}else{$status=1;}
        return
$status;
    }
    public function
print_all_time(){ //Print all connection and query time, if $debug is true
       
print_r($this->all_time);
    }
}
?>