Hello i am trying to make function with while loop in php but cant getting write here is my code
function mail_detail($mail_detail){
$data= mysql_query("select * from messages where messages.to = '$mail_detail' and to_viewed = 0 ORDER BY messages.id DESC");
while ($result= mysql_fetch_array($data)){
return $result;
}
}
and out put is
$mail_detail= mail_detail($userid)
echo '<li class="read">
<a href="#">
<span class="message">'. $mail_detail['title'].'</span>
<span class="time">
January 21, 2012
</span>
</a>
</li>';
i am not getting all values just getting one value please help
thx
The
returnstatement is terminating your loop and exiting the function.To get all values, add them to an array in the loop, and then return the array. Like this:
on the side that receives the array
To add on, a function is only able to return once (except for some special circumstances that you should not worry about). Therefore, the first time your function comes across a return statement, it returns the value and exits.
This functionality of
returncan often be used to your advantage. For example: