Everytime you call the function, it re-runs the sql, and gives you the same
list. The only time it wouldn't do that was if the id wasn't found. You
could probably change the function to a class, where the constuctor ran the
sql, and then you had another method to get the next entry.
----- Original Message -----
From: "Justin French" <[EMAIL PROTECTED]>
To: "php" <[EMAIL PROTECTED]>
Sent: Sunday, August 25, 2002 11:12 AM
Subject: [PHP] trouble with function
> Hi,
>
> This is the first time I've REALLY tackled multi-dimensional arrays, in
> conjunctions with functions.
>
> I like the way this code works:
>
> <?
> $sql = "select from....";
> $result = mysql_result($sql);
> while($myrow = mysql_fetch_array($result))
> {
> echo $myrow['colname'];
> }
> ?>
>
> So I built a function which returns a multi-dimensional array:
>
> <?
> function getSongByArtist($artist_id,$order='title ASC')
> {
>
> $sql = "
> SELECT *
> FROM songs
> WHERE artist_id='{$artist_id}'
> ORDER BY {$order}
> ";
> $result = mysql_query($sql);
> if(!$result)
> {
> return 0;
> }
> else
> {
> while($myrow = mysql_fetch_array($result))
> {
> foreach($myrow as $k => $v)
> { $$k = $v; }
>
> $songs["$id"] = array(
> 'title' => "$title",
> 'writers' => "$writers",
> 'video' => "$video",
> 'artist_id' => "$artist_id"
> );
> }
> return $songs;
> }
> }
> ?>
>
> I can then do:
> <?
> $songs = getSongByArtist(4);
> print_r($song);
> ?>
> ... and it prints the results I'm expecting. All good.
>
>
> 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?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php