newb Posted November 27, 2011 Share Posted November 27, 2011 How can i use return to output more than one row of information? i would like to merge the results from multiple SQL rows into one string somehow so that I can return them through a function. Any ideas how i can do this? here is my current code: $query = exec_mysql_query("SELECT * FROM av_genres WHERE id = $cid ORDER BY name DESC"); while ($row = mysql_fetch_assoc($query)) { return $row['name']; } if i use 'echo' it displays all results as i want it to but it doesnt output in the correct place for some reason. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/251879-how-do-i-use-return-to-output-more-than-one-row-of-information/ Share on other sites More sharing options...
kicken Posted November 27, 2011 Share Posted November 27, 2011 You can only return one value. If you want the results as a string, have the function build that string through concatenation and then return it. You could also store each result in an array and return the array. $ret = ''; $query = exec_mysql_query("SELECT * FROM av_genres WHERE id = $cid ORDER BY name DESC"); while ($row = mysql_fetch_assoc($query)) { $ret .= $row['name'].', '; } return $ret; Quote Link to comment https://forums.phpfreaks.com/topic/251879-how-do-i-use-return-to-output-more-than-one-row-of-information/#findComment-1291529 Share on other sites More sharing options...
floridaflatlander Posted November 27, 2011 Share Posted November 27, 2011 if i use 'echo' it displays all results as i want it to but it doesnt output in the correct place for some reason. any ideas? What do you mean by that? For example, you want it a certain spot on a web page? Quote Link to comment https://forums.phpfreaks.com/topic/251879-how-do-i-use-return-to-output-more-than-one-row-of-information/#findComment-1291552 Share on other sites More sharing options...
newb Posted November 27, 2011 Author Share Posted November 27, 2011 You can only return one value. If you want the results as a string, have the function build that string through concatenation and then return it. You could also store each result in an array and return the array. $ret = ''; $query = exec_mysql_query("SELECT * FROM av_genres WHERE id = $cid ORDER BY name DESC"); while ($row = mysql_fetch_assoc($query)) { $ret .= $row['name'].', '; } return $ret; exactly what i needed, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/251879-how-do-i-use-return-to-output-more-than-one-row-of-information/#findComment-1291615 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.