On Mon, 26 Aug 2002 01:12:56 +1000, you wrote:

>However, I was hoping to use it in a similar way that I use mysql in the
>above code... something like:
><?
>while($song = getSongByArtist(4))
>    {
>    echo song['title'];
>    echo song['writers'];
>    echo song['video'];
>    echo song['artist_id'];
>    }
>?>
>
>But it's just running through an infinite loop.  Perhaps mysql_fetch_array()
>is doing something magic???  Perhaps I need to return something different in
>my function?

Your function (which I've snipped for space considerations) is
populating a multidimensional array with an entire result set.  So if
your artist as 4 songs, you'll get a 2 dimensional array where the
first dimension has 4 elements.  (For that reason, your function
really needs to be caled getsongSbyartist, since it gets all the songs
an artist has.)  In the code above you want to treat your function as
if it's returning a one dimensional array containing only one row from
the result set.

You're right, you could get the return value from your function, then
use:

$songs = getSongByArtist(4);

foreach($songs as $id => $song) {
  echo song['title'];
  ...
}

But if you really want to use it in a similar fashion to the way
mysql_fetch_array() works, you're going to have to change what your
function returns.  You can use a static variable to store the state of
the result set inside your function, and only return one row at a
time.  For example:

static $myrow;
if($myrow = mysql_fetch_array($result)) {
  foreach($myrow as $k => $v) { $$k = $v; }
  $song = array ( 'title' => ...);
  return $song;
} else {
  return false;
}

That way your function remembers the position of the result set
between calls, and returns one song at a time, until there are no more
left, and then it returns false.  I haven't tested the above, but it
should behave the way you expect.

HTH

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to