Manage multiple query results in an array.
Posted: Wed Apr 23, 2008 9:27 am
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:
then the class file (snipped for relevance sake):
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!
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
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
}
Any help would be muchly appreciated!