lofaifa Posted March 23, 2012 Share Posted March 23, 2012 question of perfomance .. //take the names from table1 apply($sql1); foreach($names as $name){ //grab other info about the current user from table2 apply($sql2); } is this fine ? Quote Link to comment https://forums.phpfreaks.com/topic/259571-is-it-fine-to-apply-sql-in-a-foreach-loop/ Share on other sites More sharing options...
thomasw_lrd Posted March 23, 2012 Share Posted March 23, 2012 You could use a join. I think that would be more efficient, but I'm not positive. Quote Link to comment https://forums.phpfreaks.com/topic/259571-is-it-fine-to-apply-sql-in-a-foreach-loop/#findComment-1330532 Share on other sites More sharing options...
lofaifa Posted March 23, 2012 Author Share Posted March 23, 2012 i tried with join yesterday man and it cuzed problems and i just give up when i tried this , join adds the tables together and the result is a lot of repeated things that i dont want ! Quote Link to comment https://forums.phpfreaks.com/topic/259571-is-it-fine-to-apply-sql-in-a-foreach-loop/#findComment-1330534 Share on other sites More sharing options...
nomis Posted March 23, 2012 Share Posted March 23, 2012 sounds like you need to link up all the common fields in the two tables. Just joining them will give you cartesian results Quote Link to comment https://forums.phpfreaks.com/topic/259571-is-it-fine-to-apply-sql-in-a-foreach-loop/#findComment-1330540 Share on other sites More sharing options...
codebyren Posted March 23, 2012 Share Posted March 23, 2012 join adds the tables together and the result is a lot of repeated things that i dont want ! It sounds like you are using SELECT * in your SQL statement and/or have content duplicated across your tables (which ideally you shouldn't). Why not just select the stuff you want? Then there won't be any repeats. Something like: SELECT table1.user_id, table1.email, table2.name, table2.address FROM table1 JOIN table2 ON table1.user_id = table2.user_id WHERE table1.user_id = 'whatever' Quote Link to comment https://forums.phpfreaks.com/topic/259571-is-it-fine-to-apply-sql-in-a-foreach-loop/#findComment-1330569 Share on other sites More sharing options...
kicken Posted March 23, 2012 Share Posted March 23, 2012 is this fine ? As mentioned, you should be doing a JOIN and pulling all your results at once. Generally speaking running a query inside a loop is a bad idea and highly inefficient. If you need help putting together a query you'll have to post your table structures and some sample data and what it is your trying to retrieve. Quote Link to comment https://forums.phpfreaks.com/topic/259571-is-it-fine-to-apply-sql-in-a-foreach-loop/#findComment-1330576 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.