<?php
/*
* Define the variables
*/
define('_SERVER_','localhost');// server name
define('_USER_','www');// user name
define('_PASSWORD_','tere');// password
define('_DB_','test');// database name
define('_DEBUG_',false);// do we want to debug
/*
* This is Lazy initialization pattern class for database
* It makes only then connection when the query is requested
* It uses adodb packages
*/
class db
{
    protected 
$db;// database connection handler
    
protected $db_type// database type
    
protected $counter=0// counts the querys
    /*
    * @string to set database type: mysql, mssql, oracle
    */
    
function __construct($db_type)
    {
        
$this->db_type $db_type;
    }
    
/*
    * This method makes the connection to the database
    */
    
public function connect()
    {
        
$this->db = &NewADOConnection($this->db_type);
        
$this->db->Connect(_SERVER__USER__PASSWORD__DB_) or die("Connection to mysql database failed!! ");
        
$this->db->debug _DEBUG_;
        
$this->db->SetFetchMode(ADODB_FETCH_ASSOC);        
    }
    
/*
    * This is method to make the database query
    * @sql is sql syntax string
    */
    
public function query($sql)
    {
        if(
is_object($this->db))
        {
            
$rs $this->db->Execute($sql);
            
$this->counter++;
            return 
$rs;
        }
        else
        {
            
$this->connect();
            
$this->query($sql);
        }
    }
    
/*
    * @returns number of querys made to the database
    */
    
public function getQueryCount()
    {
        return 
$this->counter;
    }
}
/*
* Test case
*/
$mssql = new db('mssql');// db object set but connection not made
$mysql = new db('mysql');// db object set but connection not made 

$mysql->query("SET NAMES 'utf8'");// if the connection not done then make it
$mysql->query("SElECT * FROM 'boo'");// connection exists just make the query and count it

echo $mssql->getQueryCount();// returns 0
echo $mysql->getQueryCount();// returns 2
?>