Life of a web developer

November 22, 2010

Tutorial: Handy mysql database object

Filed under: Database, PHP, Tutorials — Tags: , , , , — Ole Aass @ 10:45 pm

I’m tired of typing the same stuff over and over. So I spend alot of time writing reusable code.

No matter if it’s just simple methods or abit more complex objects. Here’s the database object I just wrote. It’s versatile and it really doesnt restrict you to follow any special rules except what objects already restricts you to. You can decide for yourself if you want to complete the query statement or make prepared statement in a valid vsprintf() format.

The following code is examples of usage:

$db = new Class_Database;
$db->connect();

$sql = "SELECT * FROM users WHERE username='%s' AND password='%s'";
$inputs = array('username', 'password');

$result = $db->execute($sql, $inputs);
if (!$result)
{
	return false;
}
else
{
	return true;
}

or you can use:

$db = new Class_Database;
$db->connect();

$username = $db->secureInputs('username');
$password = $db->secureInputs('password');
$sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."'";

$result = $db->execute($sql);
if (!$result)
{
	return false;
}
else
{
	return true;
}

(more…)

Blog at WordPress.com.