Jump to content

multiple table query


Aezel

Recommended Posts

HI,Please excuse me if the answer to my question has already been posted but i couldn't find it. I'm new to SQL and i have a problem with doing a query from two different sql tables onto the same php page.One table is "headlines" which should display the 3 most recent table entries and the other table is "events" which should display the latest 4 table entries.I made the headlines table and sql/php code first and it all worked fine on the php page where the sql data is called with SELECT.After that i added the events table and now the target php page displays only the most recent "headlines" entry 3 times (the same entry 3 times), and the most recent "events" entry is displayed only once..This is the sql code on the php target page i have now:

<?phprequire("/mydb");$sql = new MySQL_class;$sql->Create("mysite");$sql->QueryRow("SELECT * from headlines ORDER BY headlines_id DESC LIMIT 3");for ($i = 0; $i < $sql->rows; $i++) { 	$sql->Fetch($i); 	$date = $sql->data[1]; 	$user = $sql->data[2];	$message = $sql->data[3];$sql->QueryRow("SELECT * from events ORDER BY month, day LIMIT 4");for ($i = 0; $i < $sql->rows; $i++) { 	$sql->Fetch($i); 	$weekday = $sql->data[1]; 	$month = $sql->data[2];	$day = $sql->data[3];	$type = $sql->data[4];	$location = $sql->data[5];	$comment = $sql->data[6];	?>

The first query worked perfectly on its own. The problem started when i added the second.How do i combine the 2 $sql->QueryRow parts so both are called correctly at the same page?I'm a total noob at this :). So far I have only used existing code that a friend made once and modified it to work with my own tables. But now i need some expert counselling.Any help would be greatly appreciated :).

Link to comment
Share on other sites

Thank you for replying sbrownii but I'm afraid you're gonna have to spell it out for me. I really don't know much about SQL at all and have only been modifying existing code to work with my own tables.What do u mean by closing for loops?

Link to comment
Share on other sites

My comment is about your php, not your sql.you open 2 for loops in your code similar to this:

for ($i = 0; $i < $sql->rows; $i++) {

the "{" makes the for loop contain everything after it until it finds "}" (which closes the loop) - but you didn't close your loops. If you just meant for the line immediately following the for statment, you could remove the "{", but it is generally better practice to always use "{" and "}" to enclose the statements you want repeated.You will have to understand what the code is doing in order to decide where to close your loops. Maybe you should talk to your friend...If you don't understand this, you should read through a tutorial or reference about control structures in php http://us2.php.net/manual/en/language.control-structures.php

Link to comment
Share on other sites

Hehe that's how new I am to this; I posted in the wrong forum :). Sorry for that.Thanks for your help, I solved the problem. I did close the loop before, at the end of the "headlines" table that is generated with the first query.I made the mistake of placing the query for "events" inside the same loop as the "headlines". So I now moved the "events" query to the table where that specific data has to be generated and it all works :).I already had a close loop "}" at the end of the "events" table, I'm guessing that's why the misplaced query did return one result at the designated table, even though the query was in the headlines loop.Anyways thanks again, it works like a charm now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...