Manage multiple query results in an array.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Manage multiple query results in an array.

Post by markusn00b »

I have written a connection class, for mysql. Now, in that class i have a function for setting a query, and then a function for running a query. The way i did this was by running set_query("query goes here...") this would then set the result of the query (mysql_query("query goes here...")) into a variable named Result. However, i came across a problem: what if there's more than one query to be executed? set_query() within a previous mysql query will overwrite the previous query!

I had an idea to use arrays and this is the idea in PHP:
in a php file:

Code: Select all

 
$DB->set_query("SELECT `Visit_Count` FROM `{$DB->Table}`", 'GET_VISIT_COUNT');
$DB->set_query("SELECT `Visit_Count_Unique` FROM `{$DB->Table}`", 'GET_VISIT_COUNT_UNIQUE');
// @param: query to be executed
// @param: identifier for query
 
then the class file (snipped for relevance sake):

Code: Select all

 
<?php
// snipped 
class DB {
    // snipped
   /*
    | set_query() assigns the necessary statements
    | for later use
    */
    public function set_query($_Query, $_Query_Id = NULL)
    {
        $this->Query['Query'][]     = $_Query;
        $this->Query['ID'][]        = $_Query_Id;
        $this->Query['Result'][]    = mysql_query($this->Query[$_Query]);
    }
    
    
    /*
    | run_query() returns the query that was passed
    | in set_query() so it can be used in a way
    | that wont give a 'isnt valid resource' warning
    */
    public function run_query($_Query_Id)
    {
        return $this->Query['Result'][0];
    }
    
    // snipped
  
}
 
This simply takes the Identifier and the Query, puts them into an array and tries to execute them - i know this code isn't working, but i'm unable to think of a way to do this!

Any help would be muchly appreciated!
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Manage multiple query results in an array.

Post by markusn00b »

Ok, so i have changed the set_query() function to:

Code: Select all

 
    public function set_query($_Query, $_Query_Id = NULL)
    {
        $this->Query[$_Query_Id]['Query']   = $_Query;
        $this->Query[$_Query_Id]['Result']  = mysql_query($this->Query[$_Query]);
    }
 
Make's grabbing the query easier.
Post Reply