TRobs Posted March 3, 2006 Share Posted March 3, 2006 First off, I would just like to say "hi" as this is my first post here.Here is the code[code]// GET GUILD INFORMATION$guild_info_query = mysql_query (" SELECT * FROM `guilds` WHERE `name` = '$guild' LIMIT 0,1") or die (mysql_error ());$guild_info = mysql_fetch_array ($guild_info_query);$guild_id = $guild_info['id'];$member_info_query = mysql_query (" SELECT `id`, `username`, `level` FROM `users` WHERE `guild` = '$guild' ORDER BY `username`") or die (mysql_error ());$member_info = mysql_fetch_array ($member_info_query);print_r ($member_info);?><table> <tr> <td>Guild Name:</td> <td><?php print $guild; ?></td> </tr> <tr> <td>Guild Leader:</td> <td><?php foreach ($member_info as $leader) { if ($leader['id'] == $guild_id) { print $leader['username']; } } ?></td> </tr> <tr> <td>Description:</td> <td><?php print $guild_info['description']; ?></td> </tr> <tr> <td colspan="2">Members</td> </tr><?php foreach ($member_info as $member) { print "<tr><td> <a href=\"viewcharacter.php?view=".$member['id']."\">".$member['username']."</a></td><td>".$member['level']."</td></tr>"; }?>[/code]The first problem I am having has to do with the second MySQL query. While testing I have 3 people in the guild. [i]$guild[/i] is defined in an included file. When I run the query through phpMyAdmin it works the way i want it too, but in this file it returns only one of the guild members and it adds new things to the array from which im not sure where they come.Here is what the print_r function returns:Array ( [0] => 2 [id] => 2 [1] => Drew [username] => Drew [2] => 1 [level] => 1 )My other Problem takes place in both of the foreach loops. In the first loop I want it to print the username of the person in the guild whose user_id == the guild_id. Instead it prints all the numbers in the array the equal the guild_id.Second foreach loop should print the user name and their level, but instead it goes through each variable in the array and just prints the first letter or number and goes onto the next.Thanks for the help in advanced. I am willing to try any alternatives to the foreach loops Quote Link to comment https://forums.phpfreaks.com/topic/3968-mysql-query-and-foreach-loop-not-acting-as-wanted/ Share on other sites More sharing options...
wickning1 Posted March 3, 2006 Share Posted March 3, 2006 mysql_fetch_array() only returns one row at a time.You have to use something likewhile ($leader = mysql_fetch_array($member_info_query)) { // do your output}to get all the rows. Quote Link to comment https://forums.phpfreaks.com/topic/3968-mysql-query-and-foreach-loop-not-acting-as-wanted/#findComment-13768 Share on other sites More sharing options...
TRobs Posted March 3, 2006 Author Share Posted March 3, 2006 awsome, thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/3968-mysql-query-and-foreach-loop-not-acting-as-wanted/#findComment-13800 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.