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 Quote 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. Quote 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 :| Quote 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 Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.