PHP Returning Multiple Arrays via Class Function Call
Posted: Wed May 18, 2005 9:15 pm
What's up people. I have been working on a class, and I've run into a wierd problem. Basically, I have a function that returns an associative array from a mysql query.
Example :
Now, this is a very basic version of it, but it's enough to get the point of what the function does.
The problem lies within the fact that if I send it a field once to query and return an array of rows back from, it works fine. But, when I send it another field, it returns a blank array.
Example
the first array ($first_array) returns with rows of date populated, but $second_array of time returns blank.
i understand that when mysql_fetch_assoc is called, it queries the sql statement, returns what arrays are called, and then frees the query. But, what if you are wanting to do what I am trying to do?
Obviously an easy work around is to just send an array of fields that are to be populated into the $row array
example :
and then return the result as a multi-dimensional array and then just loops through it assigning where it is needed, however, I want to know how I can do what I was intending in the first example, by setting 2 seperate occurances into 2 seperate variables that will return with the rows I need.
Another solution would be to just write the function to where it fetchs and all fields, builds arrays for all fields, and then returns only the fields that are set in the $a->fetch_assoc($field) call...
Again, I am just being a hardass about this and basically am just wondering if there is a solution to do it the way I had originally intended. Thanks
Example :
Code: Select all
function fetch_assoc($field)
{
while($row=mysql_fetch_assoc($this->sql))
{
$field_data[] = $row[$field];
}
return $field_data;
}The problem lies within the fact that if I send it a field once to query and return an array of rows back from, it works fine. But, when I send it another field, it returns a blank array.
Example
Code: Select all
include('myclass.php');
$a = new MyClass;
$a->connect();
$a->select_db('whatever');
$a->query('select date,time from mytable');
$first_array = $a->fetch_assoc('date');
$second_array = $a->fetch_assoc('time');i understand that when mysql_fetch_assoc is called, it queries the sql statement, returns what arrays are called, and then frees the query. But, what if you are wanting to do what I am trying to do?
Obviously an easy work around is to just send an array of fields that are to be populated into the $row array
example :
Code: Select all
$my_fields = $a->fetch_assoc('field','field2');Another solution would be to just write the function to where it fetchs and all fields, builds arrays for all fields, and then returns only the fields that are set in the $a->fetch_assoc($field) call...
Again, I am just being a hardass about this and basically am just wondering if there is a solution to do it the way I had originally intended. Thanks