Forgot Password / Register
Site Statistics
Total Members: 509
Total Tutorials: 242
Newsest User: Lestop
Todays Unique Hits: 51
0 Users 2 Guests Online

Advanced mySQL OOP Class

First off you may want to organize your main directory and sub directories so that you can find all your things easier.
My Directory Structure:
Code

--Main--
--Inc
----Classes
--Lib
--Images


If you have a classes folder make this file within that directory: mysql.class.php

In this file you will want something similar to:
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
class mySQL//Define the class of mySQL
    
var $db = array(); //your database information
    
var $persistant_connection false//persistant connection
    
var $queryCounter 0//no queries :O
    
var $TotalTime 0//no time yet =D
    
var $link ""//link used for closing
    
function __Connect(){ //cvonnect to mySQL
        
$startTime $this->getMicroTime(); //script start time
                
$this->TotalTime += $this->getMicroTime() - $startTime//Add it to the total time
        
if($this->persistant_connection == 'True'){ //if persistant connection is being used

                
$this->link mysql_pconnect($this->db["hostname"], $this->db["username"], $this->db["password"]); /connect to mySQL and set the link variable
        
}else{ //or maybe not..
                
$this->link mysql_connect($this->db["hostname"], $this->db["username"], $this->db["password"]); //still connect but with no continuious connection
        
//end pers. test
        
mysql_select_db($this->db["database"], $this->link); //get database with selected Link variable
    
//End function
    
function __Disconnect(){ //Define disconnect function
        
$startTime $this->getMicroTime(); //out start time
            
mysql_close($this->link); //end the link =D
        
$this->TotalTime += $this->GetMicroTime $startTime//Add up the time
    
//End the function
    
function sqlQuery($query){ //function used for mySQL Queries
        
$startTime $this->getMicroTime(); //start time
        
$exec mysql_query($query); //execute the query
        
++$this->queryCounter//add one to query counter
        
$this->TotalTime += $this->getMicroTime() - $startTime//add up more time to the timer
        
return $exec//return the results
    
//end function
    
function numRows($sql){ //mysql_num_rows
        
$startTime $this->getMicroTime(); //start time =D
        
$this->TotalTime += $this->getMicroTime() - $startTime//add to total time
        
++$this->queryCounter//add one to query counter
        
$exec mysql_num_rows($sql); //execute the query
        
return $exec//Return the results
    
//ENd function
    
function fetchArray($sql){ //mysql_fetch_array
        
$startTime $this->getMicroTime(); //Our start time
        
$this->TotalTime += $this->getMicroTime() - $startTime//Add to total time
        
++$this->queryCounter//add one to query counter
            
$exec mysql_fetch_array($sql); //execute the query
        
return $exec//return the results
    
//end functions
    
function getMicroTime(){ //Gert our complete microtime
        
list($usec$sec) = explode(" "microtime()); //simple thing to get the time started
        
return ((float)$usec + (float)$sec); //give the start time and whatnot
    
//end function
    
function getTotalTime(){ //get our total time
        
$string round($this->TotalTime6); //round to six decimal places
        
return $string//return the time
    
//end
    
function sqlCount(){ //get total sql uqeries
        
return $this->queryCounter//return the amount of queries
    
//end function
//end class
?>


To use this you will want to open your configuration main file and type something a bit like:
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
require("mysql.class.php"); //get our class file
    
$dbinfo = array(
    
"hostname" => "localhost",
    
"username" => "root",
    
"password" => "",
    
"database" => "sqltest",
        ); 
//all your database information
$MySql = new mySQL//create a new mySQL Clss for the var: $MySql
$MySql->db $dbinfo//set database info
$MySql->__Connect(); //and finally Connect :D
?>


Real Quick lets run a query through our phpMyAdmin:
Code

CREATE TABLE `test` (
`id` INT( 11 ) NOT NULL auto_increment,
`name` VARCHAR( 255 ) NOT NULL,
`data` VARCHAR( 255 ) NOT NULL,
PRIMARY KEY(`id`)
);

INSERT INTO `test` (`name` ,`data`) VALUES ('Bob', 'Old');


In a new file maybe named test.php you could have:
PHP Code
1
2
3
4
5
6
7
8
9
10
<?php
require("config.php");
$get_data $MySql->sqlQuery("SELECT * FROM `test` ORDER BY `id` ASC"); //get all data ordered ascending by id
while($objects $MySql->fetchArray($get_data)){ //lets loop the data
echo "$objects[name] is $objects[data]"//Couldn't think of anything else but yeah echo data
//end the loop
echo "<b>Total Queries</b>: ".$MySql->sqlCount()."&nbsp;<b>Execution Time</b>: ".$MySql->getTotalTime()."\n";
$MySql->__Disconnect(); //hehe close connection x]
?>


Please alert me of any mistakes you may find and i shall have them fixed.
ShadowMage
Author:
Views:
1303
Rating:
Posted on Tuesday 27th January 2009 at 09:22 PM
ShadowMage
ShadowMage
Works fine for me...
Posted on Tuesday 27th January 2009 at 01:20 PM
Vic Vance
Vic Vance
Hey this script does not work on come browser why is that like X10hosting