MySQL Forums
Forum List  »  PHP

MySql Loop?
Posted by: Nick Thompson
Date: April 15, 2009 05:32PM

I have a scenario where I want to put php programming logic into the sql logic. Is this possible? Let me explain my scenario.

I have two tables, courses, and categories. Here's a dummed-down version of each:

Categories:
int id, int parent, string name

Courses:
int id, int parent, string name.

Now lets say in the categories table we have the following rows:
1   0   "English"
2   0   "Science"
3   2   "Chemistry"
4   2   "Physics"
Now Courses might have the following rows:
1   3   "chem101"
2   0   "english101"
3   4   "physics101"
4   2   "science101"

Lets say I wanted to get all the courses that are under the "science" category:
$resultset = mysql_query("select 'id' from categories where 'parent'=2");

foreach($resultset as $value){
   mysql_query("select * from courses where 'parent'=$value")
   //do something with the results
}
mysql_query("select * from courses where 'parent'=2")
I'm sure that there is a way to do this looping logic with one sql statement, but I can't figure out what that is. I did see something on "cursors" is that what would solve my problem?

Thanks,
Nick

Options: ReplyQuote


Subject
Written By
Posted
MySql Loop?
April 15, 2009 05:32PM
April 15, 2009 09:17PM


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.