dannyb785 Posted July 16, 2012 Share Posted July 16, 2012 I have a site with a facebook-type newsfeed and each newsfeed post has comments and likes. So my code works like so: *query that grabs all newsfeed posts while(row = mysql_fetch_assoc()) { *query to grab this post's likes and output the number *query to gran this post's comments and output them } I have the proper columns indexed to help a little bit, but I feel helpless, is there a way to do anything close without having to do these queries within the main query? Quote Link to comment https://forums.phpfreaks.com/topic/265732-can-i-prevent-a-query-within-a-query/ Share on other sites More sharing options...
requinix Posted July 16, 2012 Share Posted July 16, 2012 Depending how the likes are stored you could grab them with the first query (the one you aren't showing). Otherwise not really: you'll need three queries somewhere, be they subqueries or not. Quote Link to comment https://forums.phpfreaks.com/topic/265732-can-i-prevent-a-query-within-a-query/#findComment-1361795 Share on other sites More sharing options...
cpd Posted July 16, 2012 Share Posted July 16, 2012 I don't see why your not putting the "grabs newsfeed posts" and "grabs post's likes" in a single query - it would be a nested select but it would be tiny. In terms of nested loops which you'd have with the third query, why not retrieve all the data at the top of the page or where ever the logical place is for you, build a multidimensional array and cycle through the array as opposed to doing the queries on the fly sort of thing. Quote Link to comment https://forums.phpfreaks.com/topic/265732-can-i-prevent-a-query-within-a-query/#findComment-1361824 Share on other sites More sharing options...
dannyb785 Posted July 17, 2012 Author Share Posted July 17, 2012 I thought about that but was worried that putting that much data into php variables would be slow as well, but it's certainly something to try out Quote Link to comment https://forums.phpfreaks.com/topic/265732-can-i-prevent-a-query-within-a-query/#findComment-1362230 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.