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


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

Reply via email to