MySQL Forums
Forum List  »  Newbie

Query in a loop
Posted by: anuj tenani
Date: January 20, 2009 08:54AM

hello everyone...

i am building a new website based on php,mysql

it is more like an article directory where we have categories and the articles in each category

now what i want is to display on the index page 5 articles from each category..
currently i am using

//Query 1
gets all the categories

SELECT `id` FROM `categories


now loop through the returned `id` to get 5 posts from each category from articles table

foreach($result as $id)
SELECT * FROM `articles` WHERE `category_id` = $id LIMIT 0,5 ORDER BY `views` DESC


now i have over 12 categories the loop runs 12 times which significantly increases page loading time

my question is
Is there any way to run just one query to get all the articles ???

Options: ReplyQuote

Written By
Query in a loop
January 20, 2009 08:54AM
January 20, 2009 10:49PM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.