Forum Moderators: coopster

Message Too Old, No Replies

Store data from function in variable

how to store data from user defined function into a varible

         

radiofanatic

9:06 pm on Jul 12, 2009 (gmt 0)

10+ Year Member



Hello, I have this code

function listGenres() {
$query = mysql_query("SELECT * FROM category ORDER BY name ASC");
while($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
return "<option value=\"$id\">$name</option>\n";
}
}

$listGenres = listGenres();


It stores only the last element from the query... What is wrong and how can I store in $listGenres all results returned with listGenres() funtion?

bkeep

6:18 am on Jul 13, 2009 (gmt 0)

10+ Year Member



The reason why it isn't working is the first time your loop runs it returns a value and ends any further execution.

[us3.php.net...]

try this


function listGenres() {
$query = mysql_query("SELECT * FROM category ORDER BY name ASC");

$option_rows = '';
while($row = mysql_fetch_array($query)) {
$option_rows .= "<option value=\"$row[id]\">$row[name]</option>\n";
}
if (!empty($option_rows)) {
return "$option_rows";
} else {
return false;
}
}