Help with database class design.

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

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

Post by daedalus__ »

I don't know. I've kind of been on auto-pilot lately. lol
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

With regards to multiple queries; you can create a method to register the query, then a seperate query to execute all registered queries.

Akin to:

Code: Select all

class DB
{
    private $queries;
    private $results;

    public function __construct ()
    {
        $this->queries = array();
        $this->results = array();
    }

    /**
    * adds the query to the query bin
    * @param sql the query to be added
    * @return int the index of the query, which will also be the index of the result.
    */
    public function addQuery($sql)
    {
        $this->queries[] = $sql;
        return max(array_keys($this->queries));
    }

    public function commit ()
    {
        foreach ($this->queries as $index => $query) {
            $this->results[$index] = mysql_query($query);
        }
    }

    public function getResult ($index = 0)
    {
        return $this->results[$index];
    }
}
in addition to the other parts of you class. :)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Thanks Jenk. :D

I was wondering what people think of this:

Code: Select all

public function RegisterEntry($entry_type, $entry) 
{ 
        switch ($entry_type) 
        { 
                case 'error': 
                        $this->errors[count($this->errors)] = $entry; 
                        return count($this->errors)-1; 
                        break; 
                case 'result': 
                        $this->results[count($this->results)] = $entry; 
                        return count($this->results)-1; 
                        break; 
                case 'link': 
                        $this->links[count($this->links)] = $entry; 
                        return count($this->links)-1; 
                        break; 
                default: 
                        throw new Exception('Tried to register unknown or unallowed object!', 0x0f); 
                        break; 
        } 
}
Good, Bad, Ugly? I am figuring since nothing was said that it is an okay way to do that.
Post Reply