Jump to content

loop problem


proctk

Recommended Posts

below is code that I'm trying to use to get data from the listed tables and then pull the data and display some of ithe information in a table.background I have a table where people add people to there buddy list. buddies or registered users add information to their profile and when someone adds a user to their buddy list then they can see certain pieces of information. my biggest problem is looping through the buddy list and using each buddy id to search through the tables with their information attached.The code below echoes the the value but repeats the same value many times.thank you for any helpcomplete code <?php $get_buddies_id = mysql_query("SELECT * FROM buddylink WHERE owner_id ='$user_id'") or die ("Error: getting buddies user_id" .mysql_error());while ($id_row = mysql_fetch_assoc($get_buddies_id)){$buddy_id = $id_row['buddy_id']; $buddy_calendar = mysql_query("SELECT * FROM `users` AS uINNER JOIN `calendar` AS c ON u.user_id = c.useridINNER JOIN `children` AS C ON u.user_id = C.owner_idINNER JOIN `parent` AS p ON u.user_id = p.owner_idINNER JOIN `sibling` AS s ON u.user_id = s.owner_idWHERE u.user_id = '{$buddy_id}'ORDER by u.user_id;") or die ("Error: getting buddies user_id:<br>\n" .mysql_error()); while ($row_buddy_info = mysql_fetch_array($buddy_calendar))echo $row_buddy_info['first_name'];}}?>

Link to comment
Share on other sites

It's generally not a good practice to put database connections in loops. It would be better if you could alter your SQL so that you only made one or two calls to the database - outside of the loops.For example, if your first query returned a list of IDs for the buddies, you could loop through those results to convert them to a comma delimited string. Then, you could call your database again with the WHERE clause something like: WHERE u.user_id IN (3,6,12,24,42)rather thanWHERE u.user_id = 3Then, when you get those results, you can loop through them to display them on your page. This keeps the database calls outside of the loops and minimizes the number of calls that need to be made to the database.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...