Forgot Password / Register
Site Statistics
Total Members: 510
Total Tutorials: 242
Newsest User: Hp
Todays Unique Hits: 121
0 Users 2 Guests Online

mySQL Class

Ever get tired of writing:
Code

or die(mysql_error());


on all your mySQL queries? If you sue this you wouldn't have to do that anymore..

Though in order to use this you would have to go through your scripts and fix the SQl queries and all that fun stuff.

First off, make a new file named: mysql.class.php

In this file type something like the following:
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
<?php
class mySQL //define the class
var $data = array(); //variable to array the data base info.
var $persistent_connection false//Persistent connection
var $query = array(); //array the queries coming in..
function connect(){ //the connection function
if($this->persistent_connection == 'false'){ //check if persistent connection is true or not
$link mysql_connect($this->data["host"], $this->data["user"], $this->data["pass"]) or die(mysql_error()); //use this if not
}else{ //or..
$link mysql_pconnect($this->data["host"], $this->data["user"], $this->data["pass"]) or die(mysql_error()); //use this if it is..
//end persistent connection check
mysql_select_db($this->data["data"], $link); //select the database with above details..
//end the connect function
function query($sql){ //define the query function
$Query mysql_query($sql) or die(mysql_error()); //do the query or die
return $Query//return with the results
//end the query function
function fetchData($sql){ //the fetchData function
$Query mysql_fetch_array($sql) or die(mysql_error()); //fetch the array of data or die with an error..
return $Query//return with the results.
//end the fetch data 
function numRows($sql){ //number the rows
$Query mysql_num_rows($sql) or die(mysql_error()); //number the data sent or die with an error
return $Query//return results..
//end the function
function close(){ //close the mySQL connection function
mysql_close(); //close it ^^
//end function
//end the class
?>

With that done in your configuration file you will need to redo everything sorta like the following..

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
include("mysql.class.php"); //get the mySQL Class File
$data = array( //array data
    
"host" => "localhost"//host
    
"user" => "root"//username
    
"pass" => "pw"//password
    
"data" => "test"//the database..
    
); //end array
$mySQL = new mySQL//define a new class
$mySQL->data $data//the data for connecting to mySQL
$mySQL->connect(); //connect to the database
?>


Thats it for files!

Now to use this Class you will need the following for regular queries:
Code

$mySQL->query("QUERY HERE");


mysql_num_rows would be:
Code

$mySQL->numRows("QUERY OR VARIABLE HERE");


mysql_fetch_array finally is:
Code

$mySQL->fetchData("QUERY OR VARIABLE FOR DATA HERE");


Then on the end of your files i would have:
Code

$mySQL->close();

Just to close the connections :)

Well, thats it for now! Have fun!
ShadowMage
Author:
Views:
1746
Rating:
Posted on Monday 26th January 2009 at 08:59 PM
ShadowMage
ShadowMage
Nope. only once. say you have a header/footer setup. just place it at the bottom of footer.php
Posted on Monday 26th January 2009 at 12:24 PM
Vic Vance
Vic Vance
So every script that you code at the bottom you need to have $mySQL->close();
??
Posted on Tuesday 23rd October 2007 at 09:55 PM
darklight19
darklight19
If you sue this you wouldn't have to do that anymore..


lol
Posted on Sunday 15th July 2007 at 06:01 AM
gbt91
gbt91
nice