Smee Posted February 18, 2009 Share Posted February 18, 2009 Hey All, I'm new to PHP and was wondering if someone could help me out. Im currently creating a friends list that shows added friends to users that are logged in. <?php $result = mysql_query ("SELECT * FROM friends WHERE me = '$username'"); ($row = mysql_fetch_array($result)); echo " <b>Username:</b> $row[friend] </br> "; ?> Above is the query i created that shows my freinds but because of the setup of SQL database i am only getting one result showing. SQL database is setup like this: me friend username friendsusername When multiple usernames are the same it only shows the most current friend when displayed on my main page. I have tried to do some while loops but to no avail. Bascally i just need to keep it as simple as possible but display all friends under a certain username. Thanks for any help Smee Link to comment https://forums.phpfreaks.com/topic/145777-how-to-loop-a-query/ Share on other sites More sharing options...
premiso Posted February 18, 2009 Share Posted February 18, 2009 while ($row = mysql_fetch_assoc($result)) { echo " <b>Username:</b> {$row['friend']} </br> "; } A while loop is what you were after. Link to comment https://forums.phpfreaks.com/topic/145777-how-to-loop-a-query/#findComment-765375 Share on other sites More sharing options...
Prismatic Posted February 18, 2009 Share Posted February 18, 2009 edit - dangit premiso :| Link to comment https://forums.phpfreaks.com/topic/145777-how-to-loop-a-query/#findComment-765377 Share on other sites More sharing options...
Smee Posted February 18, 2009 Author Share Posted February 18, 2009 Wow that was easy.. Thanks for that Cant belive all i didn't put was the {} around $row['friend'] when i was trying so many variations! dont suppose you could explain why they are needed there? Thanks again Link to comment https://forums.phpfreaks.com/topic/145777-how-to-loop-a-query/#findComment-765384 Share on other sites More sharing options...
marcus Posted February 18, 2009 Share Posted February 18, 2009 You also forgot to make it a while loop, lol. The curly braces are just another form of escaping variables. Link to comment https://forums.phpfreaks.com/topic/145777-how-to-loop-a-query/#findComment-765388 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.