A MySQL data access class
You may be asking yourself what the advantage of using a class over the built in native functions for the particular databases is in PHP. It’s a fair question. Why use a class at all, when you don’t have to? Right?
There are a few really good reasons for doing it. The way I see it, a class is better for data access because it can be changed, tweaked, or switched out entirely for something else. Native functions like the ones for SQLite or MySQL, or a number others can’t be changed. So depending on how you write your application, you may or may not be in trouble if the need ever arises for a new database platform.
The need could be simple. In the case of the SQLite data access class, you might start hitting your limitations on size per record. It’s also possible that your app has done so well, that your file size is now in the gigabytes, and SQLite is no longer a practical solution. Or, perhaps your boss figures out that you’re using a database type that he’s never heard of and doesn’t understand, so you’ll be asked to change it to a “sanctioned” database system. Thankfully, since you’re using ANSI SQL, the migration tasks are pretty simple.
Now the trouble becomes hot swapping one set of classes for another. Let’s revisit our interface code from the last example, and simply change the class instance.
Change:
$db = new litedb;
to:
$db = new mydb;
using this class:
<?php
class mydb
{
//the variable generated by the config.php file
public $connection_string;
//database connection
public $db;
//authentication and selection array from config.php
public $c;
//selected database
public $sdb;
//generic query
public $qq;
//generic row
public $rs;
//generic token
public $token;
//generic array
public $runQuery;
//generic increment
public $i;
function config()//pull the config file and send it out to the open function.
{
/*
connection variables are as follows:
0 = database host
1 = username
2 = password
3 = database to select
*/
$connection_string = array(‘database_host’, ‘database_username’, ‘database_password’, ‘database_name’);
return $connection_string;
}
/*
open() does the following
1. checks to see if the db is already open. that’s what if($sdb) is for.
2. obtains database configuration information
3. connects to the specified database
4. selects the specified database
*/
function open()
{
if (!$sdb)
{
global $db;
global $sdb;
$c = $this->config();
$db = mysql_connect($c[0], $c[1], $c[2]) or die(mysql_error());
$sdb = mysql_select_db($c[3], $db) or die(mysql_error());
}
return $sdb;
}
function q($token)//is what it sounds like
{
//set qq to global, just in case
global $qq;
//open the database
$this->open();
//run your query based on your generic token or arbitrary value
$qq = mysql_query($token);
if (mysql_num_rows($qq) > 0)
{
//check to see if you got anything back
//if you did, set the increment to 0 and begin your while loop.
$i = 0;
while ($rs = mysql_fetch_array($qq))
{
//throw the entire row into the runQuery array
$runQuery[$i] = $rs;
//increment i
$i++;
}
//return the full array of goodness.
return $runQuery;
}
else
{
//but make sure to return the term ‘nothing’ if that’s all you find.
return ‘nothing’;
}
}
}
?>
You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

