two queries on two tables on one page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

two queries on two tables on one page

Post by Wilbo »

Hi there,

Basic question I'm sure but how do I do two queries on two different tables on the same page?
Basically I've got a news page where people can leave comments.
The first query gets the story from the 'news' table and then below that a second query retrieves the comments posted from the 'comments' table.
Well, thats what I want, but it won't let me!
The problem seems to be putting the results of the second query into an array (mysql_fetch_array).
Incedently it works fine if the second query retrives data from the same table as the first, but that's not what I need!

Anyway I'm sure it's something simple I'm not doing right.
Thanks in advance.
lordofgore
Forum Newbie
Posts: 4
Joined: Tue Aug 19, 2008 7:31 am

Re: two queries on two tables on one page

Post by lordofgore »

post code
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

Re: two queries on two tables on one page

Post by Wilbo »

The code is pretty simple:

//news story
$query = "SELECT news_title, etc FROM news WHERE ... ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

echo '**some stuff to print the new story**';


//comments
$query = "SELECT comment_title, etc FROM comments WHERE ... ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {

echo '**some stuff to print the comments**';

}

Ive tried it with different names for the second set of variables ($query, $result, $row) but it doesn't make any difference.
I've also tried mysql_free_result().

Could it be something to do with connecting to the database?
I'm including a connection page at the top using require_once().
Any ideas?
Wilbo
Forum Newbie
Posts: 22
Joined: Fri Jul 25, 2008 5:45 am

Re: two queries on two tables on one page

Post by Wilbo »

OK, I was being an idiot, the table I was trying to query had an error in it.

Thanks
Post Reply