Forum Moderators: coopster
Is it possible for a function to return an array? I've created a function that does a SELECT from the DB. Then I need to echo the returned values. Trouble is, its just the one variable that is being returned. I need all the variables to be returned..
The code below does not work..
-- code --
function values($ID)
{
$SqlSelectQuery =
("
SELECT
name,
filename
FROM
table
AND
ID = '$ID'
");
// Perform Query
$SqlSelectResult = mysql_query($SqlSelectQuery);
while ($SqlSelectRow = mysql_fetch_assoc ($SqlSelectResult))
{
$name = $SqlSelectRow['name'];
$filename = $SqlSelectRow['filename'] ;
}
return $name;
return $filename;
}
[edited by: HoboTraveler at 6:51 am (utc) on Oct. 17, 2006]
Just simply return the array created by the query, no need for a loop. mysql_fetch_assoc is already the array you need.
function values($ID)
{
$SqlSelectQuery =
("
SELECT
name,
filename
FROM
table
AND
ID = '$ID'
");// Perform Query
$SqlSelectResult = mysql_query($SqlSelectQuery);return mysql_fetch_assoc($SqlSelectResult);
}
Then assign a variable when you want to retrieve the data:
$data = values('id number here');
print_r($data);
dc
Array (
0 => array('field1' => 'some', 'field2' => 'some'),
1 => array('field1' => 'some', 'field2' => 'some'),
2 => array('field1' => 'some', 'field2' => 'some')
...
)
So, you need to use it by calling the index for each row as:
$data[0]['name']
$data[0]['filename']
$data[1]['name']
$data[1]['filename']
Something like that, hope this helps!