Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.