PHP Returning Multiple Arrays via Class Function Call

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
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

PHP Returning Multiple Arrays via Class Function Call

Post by infolock »

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 :

Code: Select all

function fetch_assoc($field)
{
   while($row=mysql_fetch_assoc($this->sql))
   {
      $field_data[] = $row[$field];
   }
   return $field_data;
}
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

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');
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 :

Code: Select all

$my_fields = $a->fetch_assoc('field','field2');
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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

it's amazing what a little work will do. the answer is simply to mysql_data_seek and then initiate another call... duh *smacks head*. pretty funny that one can just ignore the obvious and make something a lot harder than what it really is :)

Code: Select all

function fetch_assoc($field)
{
   while($row=mysql_fetch_assoc($this->sql))
   {
       $field_data[] = $row[field];
   }
   mysql_data_seek ($this->sql, 0);
   return $field_data;
}
oh well, at least my curiosity has been solved ;)
Post Reply