Darkmatter5 Posted January 27, 2009 Share Posted January 27, 2009 Here's my code $query="SELECT mem_username AS username FROM members WHERE logged_in=1 ORDER BY mem_username ASC"; $result=mysql_query($query) or die(mysql_error()); $row=mysql_fetch_array($result); $usernames=join(", ",$row); echo "$usernames"; This only outputs the first item, why is that? Link to comment https://forums.phpfreaks.com/topic/142689-solved-help-with-using-the-join-command/ Share on other sites More sharing options...
premiso Posted January 27, 2009 Share Posted January 27, 2009 You are not doing a while loop to loop through the data... <?php $query="SELECT mem_username AS username FROM members WHERE logged_in=1 ORDER BY mem_username ASC"; $result=mysql_query($query) or die(mysql_error()); $usernames = array(); while ($row=mysql_fetch_array($result)) { $usernames[] = $row['username']; } $usernames=join(", ",$usernames); echo "$usernames"; ?> Should work. Link to comment https://forums.phpfreaks.com/topic/142689-solved-help-with-using-the-join-command/#findComment-747904 Share on other sites More sharing options...
rhodesa Posted January 27, 2009 Share Posted January 27, 2009 beat me too it...but still posting cus i wrote some comments in mine you need to loop over the records: $query="SELECT mem_username AS username FROM members WHERE logged_in=1 ORDER BY mem_username ASC"; $result=mysql_query($query) or die(mysql_error()); $usernames = array(); //Initiate the array while($row=mysql_fetch_array($result)){ //Loop over each record $usernames[] = $row['username']; //Add the username to the array } $joined = join(", ",$usernames); //Join the array to make a string echo $joined;//Print the string edit: it's scary how similar our code is being that i didn't see yours until after i wrote mine! Link to comment https://forums.phpfreaks.com/topic/142689-solved-help-with-using-the-join-command/#findComment-747905 Share on other sites More sharing options...
premiso Posted January 27, 2009 Share Posted January 27, 2009 beat me too it...but still posting cus i wrote some comments in mine you need to loop over the records: $query="SELECT mem_username AS username FROM members WHERE logged_in=1 ORDER BY mem_username ASC"; $result=mysql_query($query) or die(mysql_error()); $usernames = array(); //Initiate the array while($row=mysql_fetch_array($result)){ //Loop over each record $usernames[] = $row['username']; //Add the username to the array } $joined = join(", ",$usernames); //Join the array to make a string echo $joined;//Print the string edit: it's scary how similar our code is being that i didn't see yours until after i wrote mine! lol you know what they say? Great minds think a like lol. To be honest, whenever you beat me to the punch I think the same thing, "Damn, he wrote what I was going to" and generally discard my post lol. Link to comment https://forums.phpfreaks.com/topic/142689-solved-help-with-using-the-join-command/#findComment-747908 Share on other sites More sharing options...
Darkmatter5 Posted January 27, 2009 Author Share Posted January 27, 2009 Perfect thanks!! Link to comment https://forums.phpfreaks.com/topic/142689-solved-help-with-using-the-join-command/#findComment-747909 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.