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. Quote Link to comment 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) Quote Link to comment Share on other sites More sharing options...
mraza Posted August 1, 2010 Author Share Posted August 1, 2010 Thanks Alex for the help Quote Link to comment 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.