My Database Class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

My Database Class

Post by daedalus__ »

I'm stuck.

I'm re-designing and re-writing all the back-end stuff that I use. I started with the way my framework (i guess?) works. Now that I have settled on the design of that, I decided to start re-writing the class that I do all my Database access stuff with.

I want to be able to run multiple queries at once. I remember someone saying that lack of support for multiple queries will hurt my sites performance or something. Why not pull everything at once?

So, I want to take some queries, the database name, and whatever values need to be, like so:

Code: Select all

$sql = array(
	"INSERT INTO table1 (blah, dood, sup, lame, whoa) VALUES (null, '%s', '%s', '%s', '%s')",
	"DELETE FROM table2 WHERE field1 = '%s'",
	"SELECT * FROM table1 WHERE field1 = '%s'"
	)
$db = array(
	'whoa',
	'dood',
	'whoa'
	)
$vals = array(
	array(
	'bro',
	'chillin',
	'word'
	),
	'no',
	'yeah'
	)
I want the script to take all this and chop it up, putting into arrays, or better yet, objects. Then piece everything together, run the queries, return the full, complete query string, and the mysql resource, if there is one.

Code: Select all

// Some hawt looking arrays or object, or both working together!
This is where I am:

Code: Select all

class DatabaseLayer
{
	private	$HOST;
	private	$USER;
	private	$PASS;
	private	$DBNAME;
	public	$mysql_link;

	public	$sql;
	public	$val;
	
	public function __construct($host, $user, $pass)
	{
		$this->HOST	= $host;
		$this->USER	= $user;
		$this->PASS	= $pass;
	}

	public function Connect()
	{
		$this->mysql_link	= mysql_connect($this->HOST, $this->USER, $this->PASS);
	}

	public function Query($sql, $db, $vals = '')
	{
		$this->DBNAME	= $db;
		$this->sql		= $sql;
		$this->vals	= $vals;
		
		// I'm stuck here. I don't know what to do with this but I want to be able to run multiple queries in one go.
	}

	public function Disconnect()
	{
		mysql_close($this->mysql_link);
	}
	
	public function __destruct()
	{
		$this->Disconnect();
	}
}
Can I use objects for this sort of thing or am I restricted to arrays?
If I can use objects how do I go about doing that? Do I need to write a new class to hold this information?
Would it be better just to use arrays?

Whenever I try to think about how to make this script work I get really lost inside my head. So I am here.

Comments, questions, suggestions?

:(
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I take it that this is a really good question.. or a really bad one?

lol
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I already wrote one, I've made some changes to it since I posted it. I haven't updated it yet but you can get some ideas from it.

viewtopic.php?t=51623
Post Reply