Forum Moderators: coopster

Message Too Old, No Replies

Returning an Array from a Function

         

HoboTraveler

6:50 am on Oct 17, 2006 (gmt 0)

10+ Year Member



Hi,

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]

dreamcatcher

7:20 am on Oct 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi HoboTraveler,

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

coopster

11:24 am on Oct 17, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



... and here is the manual page for Returning values [php.net]. This will be very handy for future reference.

HoboTraveler

12:44 pm on Oct 17, 2006 (gmt 0)

10+ Year Member



@dreamcatcher,

Thank-You.. Works great!

[edited by: HoboTraveler at 12:45 pm (utc) on Oct. 17, 2006]

HoboTraveler

1:20 pm on Oct 17, 2006 (gmt 0)

10+ Year Member



Hi Guys,

There seems to be an issue where the array does not return multiple records.

Example: Instead of returning 10 records, the array holds just one record.

print_r($data);

Array
(
[ID] => 1
[name] => Foobar
)

Psychopsia

2:27 pm on Oct 17, 2006 (gmt 0)

10+ Year Member



You can do it with this code:

function values($ID)
{
$SqlSelectQuery = "
SELECT name, filename
FROM table
WHERE ID = '$ID'";

// Perform Query
$SqlSelectResult = mysql_query($SqlSelectQuery);

$data = array();
while ($row = mysql_fetch_array($SqlSelectResult))
{
$data[] = $row;
}

return $data;
}

dreamcatcher

2:55 pm on Oct 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, if your id is unique, you might want to lose the where clause.

dc

HoboTraveler

3:22 pm on Oct 17, 2006 (gmt 0)

10+ Year Member



Ok,

print_r($data); shows the array now. However, the array is blank when I echo using:

echo "$data[name]";

echo "$data[filename]";

TIA

[edited by: HoboTraveler at 3:24 pm (utc) on Oct. 17, 2006]

Psychopsia

3:31 pm on Oct 17, 2006 (gmt 0)

10+ Year Member



The returning array is:

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!

HoboTraveler

3:40 pm on Oct 17, 2006 (gmt 0)

10+ Year Member



That does not seem to work.. I need to ouput hundreds of returned records..

The print_r array returned is like this...

Array ( [0] => Array ( [0] => 1 [name] => 1 [1] => foo1 [filename] => foofile1 ) [1] => Array ( [0] => 2 [name] => 2 [1] => foo2 [filename] => foofile2 ) [2] =>

Any ideas?

TIA

dreamcatcher

7:41 pm on Oct 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A loop maybe?

for ($i=0; $i<count($data); $i++)
{
echo $data[$i]['name'];
echo $data[$i]['filename'];
}

dc

HoboTraveler

5:28 am on Oct 18, 2006 (gmt 0)

10+ Year Member



Perfect! Thanks