monkeybidz Posted July 8, 2011 Share Posted July 8, 2011 Currently I have row results showing like: word1 word2 word3 word4 How do I get them to display straight and seperate them with a comma like: word1, word2, word3, word4 Here is my current code: $query = "SELECT keyword FROM keywords ORDER BY RAND() LIMIT 1,2000"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo "$row['keyword']"; } Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/241378-need-help-with-mysql-row-results/ Share on other sites More sharing options...
xyph Posted July 8, 2011 Share Posted July 8, 2011 $out = ''; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if( !empty($out) ) $out .= ', '; // Add comma if not first entry $out .= $result['keyword']; } echo $out; unset($out); Quote Link to comment https://forums.phpfreaks.com/topic/241378-need-help-with-mysql-row-results/#findComment-1239884 Share on other sites More sharing options...
monkeybidz Posted July 8, 2011 Author Share Posted July 8, 2011 That was a no go. Results are the same. Quote Link to comment https://forums.phpfreaks.com/topic/241378-need-help-with-mysql-row-results/#findComment-1239890 Share on other sites More sharing options...
xyph Posted July 8, 2011 Share Posted July 8, 2011 How are they entered into the database? Does each keyword have its own row? Are there multiple keywords on a single row separated by line breaks? A dump of your SQL table would help while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $arr = explode( PHP_EOL, $row['keyword'] ); echo implode( ', ', $arr ); } Quote Link to comment https://forums.phpfreaks.com/topic/241378-need-help-with-mysql-row-results/#findComment-1239909 Share on other sites More sharing options...
monkeybidz Posted July 8, 2011 Author Share Posted July 8, 2011 There we go, I got the straight row, but no spaces or commas. I think this is the problem: echo implode( ', ', $arr ); Do you see something I don't? Quote Link to comment https://forums.phpfreaks.com/topic/241378-need-help-with-mysql-row-results/#findComment-1239913 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.