


This tutorial will show you the basics of creating a class that will handle your basic MySQL functions, as well as handle some error reporting so you dont have to add "or die(mysql_error());" to your mysql querys. If you have any questions or problems, just ask for help on the tutorial help forums.
<?
/* This starts the class named "sql" to handle everything */
class sql{
/* Anything with the word "var" before it in a class simply becomes a variable (or object) in the class.
The following are for the result ($result) of a mysql_query(), the connection ($conn) to the server, and the databases ($dbs) you want to use */
var $result;
var $conn;
var $dbs = array();
/* First function of the class, also known as a "constructor", can be called when you set a new object in the class (if the function has the same name as the class).
This function handles connection to the server. */
function sql($host, $user, $password, $line){
/* In class's you uses objects (which are variables with a fancy name), the current object referencing the class can be dynamically seleted using the variable "$this" */
if(!$this->conn = @mysql_connect($host,$user,$password)){
/* If you don't connect to the server, die and explain what happened. */
die("sql(): Unable to connect to host.<br>
<i>MySQL Said</i>: ".mysql_error());
}
/* Returns true so you can do further handling of errors in your script */
return true;
}
/* This functions allows you to set a database to perform queries on.
$db = the name of the database, $link = the reference number you want to use so you can easily select a specific database */
function set_db($db, $link){
/* If the database you're trying to set, isn't set, then set it or die and display an error. */
if(empty($this->dbs[$link])){
$this->dbs[$link] = $db;
}else{
die("set_db(): Database $link has already been set.");
}
return true;
}
/* This function is basicly the advanaced version of the mysql_query() that you'll be using.
$query = same as in a regular query, $link = the reference number of the database you want to use (as set in set_db()). */
function sql_query($query, $link){
if(@mysql_select_db($this->dbs[$link])){
if(!$this->result = @mysql_query($query)){
die("sql_query(): Unable to query database.<br>
<i>MySQL Said</i>: ".mysql_error()."<br>
<i>Error #</i>".mysql_errno());
}
return $this->result;
}else{
die("sql_query(): Could not select database".($link ? " $link" : "").".");
}
}
}
?>
Well, thats the basics of the class, if you want to add more features it shouldnt be to hard to do, but that will be for you to find out.
Now that you have everything set up, how do you use it? Simple:
<?
/* First we set up the variables to connect to the database. */
$db_host = 'localhost'; # The host of your server, normally localhost
$db_user = 'DB_USER'; # The user name you use to connect to the server
$db_pass = 'DB_PASS'; # The password you use to connect
/* Now that you have what you need to connect, we shall connect.
First we set a new object in to the "sql" class, and since the constructor is the same as our class name, we just connect right off the bat. */
$var = new sql($db_host, $db_user, $db_pass, __LINE__);
/* Well, now you should be connected to the server, but with out setting any databases, this would be pretty useless.
So, to set a database for use, first you need to know the name of the database, then you need to pick a number to reference the database with (I choose 1 because it's easy).
You can call this for as many databases as you want, just make sure you don't use the same number twice. */
$var->set_db('DB_NAME', 1);
/* Now your connected, you have a database to use, lets to a query to test things out.
Since most people like to use something like mysql_fetch_assoc() to get their results with, you can handle this 1 of 2 ways.
Way 1: Preforming the query as is, nothing extra (change the table name for the test, and if need be, change the number also). */
$var->sql_query("SELECT * FROM `test`", 1);
/* Then to fetch the results, just do this. */
while($r=mysql_fetch_assoc($var->result)){
highlight_string(print_r($r, 1));
}
/* Way 2: most commonly used method by all, setting another variable to hold the result id. */
$sql = $var->sql_query("SELECT * FROM `test`", 1);
/* To fetch the results: */
while($r=mysql_fetch_assoc($sql)){
highlight_string(print_r($r, 1));
}
?>
Well, that should be about it for the sql class, as for using a class in general it goes somewhat like this:
1. To set an object/variable into a class all you NEED to do is this:
$object = new class_name();
2. To use a function with the class you do this:
$object->function();
where $object = the name of the variable you set in the class, and function() = the name of the function and anything else you want to use with the function.
3. To call a variable that you may have set using a function (like we do with $this->result), it would be very similar to using arrays
$object->var_to_use;
where var_to_use = the acctual name of the variable.
4. And lastly, if you continue to improve this class, or want to write one of your own, then when every you want to set a variable from within the class you would want to say $this->var_name = "blah"; simply because the class is smart enough to know $this is the current object you are using in the class and not the literal variable "$this", also "var_name" would be the name of the variable you want to set.
Enjoy youre fancy new class.