How many times do your fingers type out the commands to run a query or fetch a row? If you are performing database tasks on the majority of your pages and you have not simplified the process then you need to keep reading. Developing a database class is not very difficult, in fact there are plenty of them out there which can set you on your way. Once you start using them you will most likely never go back.
A database class should be thorough, simple, and save you time. A single command to connect or disconnect as well as most of your common functions should be included. Do not re-invent the wheel if you have a large scale project, there are plenty of pre-build APIs which will handle much more than the examples given here. Below is a simple class which can get you up and running as well as examples of how to use it, let’s take a look:
The Class:
var $host;
var $user;
var $pass;
var $database;
var $persistent=0;
var $last_query;
var $result;
var $connection_id;
var $num_queries=0;
function configure($host, $user, $pass, $database, $persistent=0)
{
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->database=$database;
$this->persistent=$persistent;
return 1; //Success.
}
function connect()
{
if(!$this->host) { $this->host="localhost"; }
if(!$this->user) { $this->user="root"; }
if($this->persistent)
{
$this->connection_id=mysql_pconnect($this->host, $this->user, $this->pass) or $this->connection_error();
}
else
{
$this->connection_id=mysql_connect($this->host, $this->user, $this->pass, 1) or $this->connection_error();
}
mysql_select_db($this->database, $this->connection_id);
return $this->connection_id;
}
function disconnect()
{
if($this->connection_id) { mysql_close($this->connection_id); $this->connection_id=0; return 1; }
else { return 0; }
}
}
Above is the bare bones, this will allow you to connect or disconnect to your database, all you would need to do to connect is add the following lines to the beginning of your code. Remember, once you are connected you will not have to do so again.
$db=new database;
$db->configure(‘localhost’, ‘username’, ‘password’, ‘database’, ‘persistent’);
$db->connect();
$c=$db->connection_id;
So now we are connected to our db, we can add more functions to our class now to make it more usable and friendly. Add the following functions:
{
mysql_select_db($database, $this->connection_id);
$this->database=$database;
}
function query($query)
{
$this->last_query=$query;
$this->num_queries++;
$this->result=mysql_query($this->last_query, $this->connection_id) or $this->query_error();
return $this->result;
}
function fetch_row($result=0)
{
if(!$result) { $result=$this->result; }
return mysql_fetch_assoc($result);
}
function num_rows($result=0)
{
if(!$result) { $result=$this->result; }
return mysql_num_rows($result);
}
function connection_error()
{
die("<b>FATAL ERROR:</b> Could not connect to database on {$this->host} (".mysql_error().")");
}
function query_error()
{
die("<b>QUERY ERROR:</b> ".mysql_error()."<br />
Query was {$this->last_query}");
}
function fetch_single($result=0)
{
if(!$result) { $result=$this->result; }
return mysql_result($result, 0, 0);
}
function clean_input($in)
{
$in=stripslashes($in);
return str_replace(array("<",">",‘"’,"’","\n"), array("<",">",""","'","<br />"), $in);
}
function unhtmlize($text)
{
return str_replace("<br />","\n", $text);
}
function escape($text)
{
return mysql_real_escape_string($text, $this->connection_id);
}
function affected_rows($conn = NULL)
{
return mysql_affected_rows($this->connection_id);
}
The Use
So the above code added into your class will give you all of the basic connection and querying tools you should need to perform most tasks. You may want to modify and add additional functions to this class. So a basic query would now look like:
Escaping…
So if we wanted to take a group of variables posted from a form and escape them all we could write a small routine using our class. What the below would do is allow us to configure an array with the names of all the variables we are expecting. If the variable exists we escape it and return the variable as it’s original name. An example would be: a user fills out a form with two fields (username, and password), the form is posted to our php script. We are expecting those two inputs so we add it to our array. We then escape those values and return them as $username and $password. Take a look:
The above code will take any number of variables passed from your form and escape them all while returning them in a variable form. It is a great function which adds security by helping prevent injections.
Wrap it up
If you are running a small site and do not require complicated functions then the above class will be more than enough to save you time in development. Do not load a large API for small sites, this will most likely hinder performance instead of help it. Remove the commands that you do not use and add ones that you do. Having a small custom class like this makes all your other tasks just that much easier.
![[del.icio.us]](http://scriptperfect.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://scriptperfect.com/wp-content/plugins/bookmarkify/digg.png)
![[dzone]](http://scriptperfect.com/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://scriptperfect.com/wp-content/plugins/bookmarkify/facebook.png)
![[Furl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/furl.png)
![[Google]](http://scriptperfect.com/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://scriptperfect.com/wp-content/plugins/bookmarkify/linkedin.png)
![[MySpace]](http://scriptperfect.com/wp-content/plugins/bookmarkify/myspace.png)
![[Newsvine]](http://scriptperfect.com/wp-content/plugins/bookmarkify/newsvine.png)
![[Propeller]](http://scriptperfect.com/wp-content/plugins/bookmarkify/propeller.png)
![[Reddit]](http://scriptperfect.com/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://scriptperfect.com/wp-content/plugins/bookmarkify/slashdot.png)
![[Spurl]](http://scriptperfect.com/wp-content/plugins/bookmarkify/spurl.png)
![[StumbleUpon]](http://scriptperfect.com/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Technorati]](http://scriptperfect.com/wp-content/plugins/bookmarkify/technorati.png)
![[Twitter]](http://scriptperfect.com/wp-content/plugins/bookmarkify/twitter.png)
![[Email]](http://scriptperfect.com/wp-content/plugins/bookmarkify/email.png)
Best Sites to Visit…
[...]is unrelated to my site but I still like to check it out. I recommend visiting it[...]…
Official Colon Cleanse…
[...]here are some links to sites that we link to because we think they are worth visiting[...]…
Games For Girls…
[...]the time to read or visit the content or sites we have linked to below the[...]…
1.) Locksmiths…
2.) [...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…
Buy Yankee Candles at Half Off…
[...]the time to read or visit the content or sites we have linked to below the[...]…
seo live case studies…
[...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…
הכרויות בטלפון…
[...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…
Flax Seeds Benefit…
[...] here are some links to sites that we link to because we think they are worth visiting. Posted in [...]…
Wrinkle Cream Critic…
[...]below you’ll find the link to some sites that we think you should visit[...]…
הכרויות בטלפון…
[...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…
Your Fitness Goals…
[...]just below, are some not really unrelated blogs than mine, however the sites are definitely worth reading[...]…
Alaskan Smoked Salmon Gifts…
[...]just below, are some totally unrelated sites to ours, however, they are definitely worth checking out[...]…
Company Website Design States It is An Incredible Piece Of Writing Keep It Up…
In your own time try to read through these a few might constitute fascination also! fyi have anyone learn about Eygpt now has much more problerms as well
…
Wedding Invitations…
[...]below you’ll find the link to some sites that we think you should visit[...]…
margarita maker…
[...]here are some links to sites that we link to because we think they are worth visiting[...]…
普拉提 pilates.普拉提减肥 Pilates Weight Loss.T恤衫T-shirt.氯醋树脂VINISOL OH.测功机 Dynamometer.shanghai escortescort,shanghai massagemassage.气液分离器Gas-liquid separator,油水分离器Oil-water separator,空气过滤器Air Filter,冰水机Chiller.公司转让Company transfers.大金中央空调Daikin central air-conditioning.
Actually It is interesting to reach this page since I saw that this post is obviously one great place where I can find lot of useful info about code.Keep it up in the next article.
curt
Nike Clogposite shoes
Your article is extremely impressive. I never considered that it was feasible to accomplish something like that until after I looked over your post.
DIYA
linkbuilding
Actually It is interesting to reach this page since I saw that this post is obviously one great place where I can find lot of useful info about code.Keep it up in the next article.
DIYA
web bot
P90X Reviews and Workouts…
[...]the time to read or visit the content or sites we have linked to below the[...]…
Your Friend & Partner…
I really think your blog is great! I’ve added a link back here; I hope that’s alright as I’d like my readers to check your site & articles out. It’s Here on my site’s blog. Always like to honor high quality content. Great job!…
6 week body makeover…
[...]below you’ll find the link to some sites that we think you should visit[...]…
Taylor Armstrong States Its A Fantastic Post Congratulations?…
Brilliant info, reckoned I would link to this dont stop……
Gyro Bowl…
[...]Check out the following sites…[...]…
Sensa Reviews and Information…
[...]the time to read or visit the content or sites we have linked to below the[...]…
diaper bags…
[...]the time to read or visit the content or sites we have linked to below the[...]…
Healthy Trim Information…
[...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…
Links……
[...]we recommend to visit the following article and it really worth checking out[...]……
Learn About Colon Cleanse…
[...]the time to read or visit the content or sites we have linked to below the[...]…
SEOXP SEO Service…
[...]below you’ll find the link to some sites that we think you should visit[...]…
Discount Folding Tables…
[...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…
Links……
[...]it is time to visit the following site and it worth checking out[...]……
baby bedding…
[...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…
[...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…
[...]the time to read or visit the content or sites we have linked to below the[...]…
Travis on his way to hollywood?!…
[...]do you know that we were looking for this, johny was too[...]…
Natasha Beddingfield into rehab, or is it just a prank?!…
[...]Britney Spears is into drugs, she’s losing custody of her kids?!…
Rob Rasner Says It’s An Incredible Article Cool!…
Top story thinking I could insert some not related data, nevertheless still worthwhile having a look…
A great blog…
[...]just below, are some totally unrelated sites to ours, however, they are definitely worth checking out[...]…
Discount Electric Guitar Packages…
[...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…
Blog Fan…
[...]just below, are some totally unrelated sites to ours, however, they are definitely worth checking out[...]…
[...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…
[...]just below, are some totally unrelated sites to ours, however, they are definitely worth checking out[...]…
how to get ripped abs fast…
[...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…
My Snoring Solution…
[...]Check out these sites[...]…
e-liquid…
[...]we like to honor other sites on the web, even if they aren’t related to us, by linking to them. Below are some sites worth checking out[...]…
Online Movie Downloads…
[...]the time to read or visit the content or sites we have linked to below the[...]…
mlm secrets…
[...]just below, are some totally unrelated sites to ours, however, they are definitely worth checking out[...]…
Travel Deals…
[...]few sites that are shown below, through our perspective are most certainly worth visiting[...]…
Free iPad 2…
[...]Get yourself a free ipad 2[...]…
Read reviews and more…
[...]even if they aren’t related to our content by Linking to it from Our Blog. Check out these sites[...]…
Watch Free Movies Online…
[...]while the sites we link to below are completely unrelated to ours, we think they are worth a read, so have a look[...]…