mraza Posted August 1, 2010 Share Posted August 1, 2010 Hi i am in a situation where i need to use two tables. please look at my code $result= $db->dbrun('select * from table_user'); while($row = mysql_fetch_array($result)): echo $row['username'] . '<br />'; endwhile; now my problem is i wants to get something from other table where user id match with $row['id'], $result= $db->dbrun('select * from table_user'); while($row = mysql_fetch_array($result)): $query= $db->dbrun('select id from table_other where user_id =$row['id']'); while($user_row = mysql_fetch_array($query)): echo $row['username'] . ' - ' . $user_row['id'] . '<br />'; endwhile; endwhile; so my main question is is it ok to run $query within a while loop because i cant run this query without $row['id'] which i gets after loop started or is there any other good way. Thanks for answering. Link to comment https://forums.phpfreaks.com/topic/209505-double-while-loop-question/ Share on other sites More sharing options...
Alex Posted August 1, 2010 Share Posted August 1, 2010 You can use a MySQL JOIN query instead of the nested while loops. Something like this would be the equivalent for your example: SELECT table_user.*, table_other.id FROM table_user JOIN ON (table_user.id = table_other.user_id) Link to comment https://forums.phpfreaks.com/topic/209505-double-while-loop-question/#findComment-1093847 Share on other sites More sharing options...
mraza Posted August 1, 2010 Author Share Posted August 1, 2010 Thanks Alex for the help Link to comment https://forums.phpfreaks.com/topic/209505-double-while-loop-question/#findComment-1093868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.