mySQL Class


Ever get tired of writing:
PHP Code
  1. 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. <?php
  2. class mySQL { //define the class
  3. var $data = array(); //variable to array the data base info.
  4. var $persistent_connection = false; //Persistent connection
  5. var $query = array(); //array the queries coming in..
  6. function connect(){ //the connection function
  7. if($this->persistent_connection == 'false'){ //check if persistent connection is true or not
  8. $link = mysql_connect($this->data["host"], $this->data["user"], $this->data["pass"]) or die(mysql_error()); //use this if not
  9. }else{ //or..
  10. $link = mysql_pconnect($this->data["host"], $this->data["user"], $this->data["pass"]) or die(mysql_error()); //use this if it is..
  11. } //end persistent connection check
  12. mysql_select_db($this->data["data"], $link); //select the database with above details..
  13. } //end the connect function
  14. function query($sql){ //define the query function
  15. $Query = mysql_query($sql) or die(mysql_error()); //do the query or die
  16. return $Query; //return with the results
  17. } //end the query function
  18. function fetchData($sql){ //the fetchData function
  19. $Query = mysql_fetch_array($sql) or die(mysql_error()); //fetch the array of data or die with an error..
  20. return $Query; //return with the results.
  21. } //end the fetch data
  22. function numRows($sql){ //number the rows
  23. $Query = mysql_num_rows($sql) or die(mysql_error()); //number the data sent or die with an error
  24. return $Query; //return results..
  25. } //end the function
  26. function close(){ //close the mySQL connection function
  27. mysql_close(); //close it ^^
  28. } //end function
  29. } //end the class
  30. ?>

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

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


Thats it for files!

Now to use this Class you will need the following for regular queries:
PHP Code
  1. $mySQL->query("QUERY HERE");


mysql_num_rows would be:
PHP Code
  1. $mySQL->numRows("QUERY OR VARIABLE HERE");


mysql_fetch_array finally is:
PHP Code
  1. $mySQL->fetchData("QUERY OR VARIABLE FOR DATA HERE");


Then on the end of your files i would have:
PHP Code
  1. $mySQL->close();

Just to close the connections :)

Well, thats it for now! Have fun!
ShadowMage's Avatar
Author:
Views:
2,546
Rating:
Posted on Monday 26th January 2009 at 08:59 PM
ShadowMage
ShadowMage's Avatar
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
VicVance
VicVance's Avatar
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's Avatar

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's Avatar
nice