trouble with function

From: Date: Sun, 25 Aug 2002 15:12:56 +0000
Subject: trouble with function
Groups: php.general 
Request: Send a blank email to php-general+get-114054@lists.php.net to get a copy of this message
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? I guess I could do it as a foreach()... Justin French

« previous php.general (#114054) next »