monkeybidz Posted April 9, 2009 Share Posted April 9, 2009 I have a query that I get results to one at a time depending on the last created item, but would like the results to display in a row separted by a comma. Is this possible? Here is my current code: $query = "SELECT title FROM XXXXX_XXXXX WHERE private='n' ORDER BY starts DESC LIMIT ".$SETTINGS['lastitemsnumber']; $result = mysql_query($query); if($result) { $num_auction = mysql_num_rows($result); $title = mysql_result($result,$i,"title"); $i++; } Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/153406-solved-sql-result-array-help/ Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 Like this? $query = "SELECT title FROM XXXXX_XXXXX WHERE private='n' ORDER BY starts DESC LIMIT ".$SETTINGS['lastitemsnumber']; $result = mysql_query($query) or die(mysql_error()); if($result) { while($row = mysql_fetch_assoc($result)) { $string .= $row['title'] . " ,"; } } echo $string; Quote Link to comment https://forums.phpfreaks.com/topic/153406-solved-sql-result-array-help/#findComment-805989 Share on other sites More sharing options...
monkeybidz Posted April 10, 2009 Author Share Posted April 10, 2009 Your on the right track, except I get the result with a , like I want, but not the first result. I will use the word "sample" as array for all result: resulst: samplesample, sample, sample, sample and so on. The first two sample's don't get a , or space. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/153406-solved-sql-result-array-help/#findComment-806060 Share on other sites More sharing options...
monkeybidz Posted April 10, 2009 Author Share Posted April 10, 2009 You lead me on the right path! Here we go: $query = "SELECT title FROM PHPAUCTION_auctions WHERE closed='0' AND suspended=0 AND private='n' ORDER BY RAND()"; $result = mysql_query($query); if($result) { while($row = mysql_fetch_assoc($result)) { $title .= ", ".$row['title']; } } Thanks! Your Awsome! Quote Link to comment https://forums.phpfreaks.com/topic/153406-solved-sql-result-array-help/#findComment-806069 Share on other sites More sharing options...
Maq Posted April 10, 2009 Share Posted April 10, 2009 That will also put a comma in front of the first extracted record. You might want something like: $title .= (!isset($title)) ? $row['title'] : ", ".$row['title']; But if the way you have works, it works... Quote Link to comment https://forums.phpfreaks.com/topic/153406-solved-sql-result-array-help/#findComment-806079 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.