Jump to content

[SOLVED] Help with using the join command


Darkmatter5

Recommended Posts

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

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.

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!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.