Page 1 of 1

two queries on two tables on one page

Posted: Tue Aug 19, 2008 8:25 am
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.

Re: two queries on two tables on one page

Posted: Tue Aug 19, 2008 8:48 am
by lordofgore
post code

Re: two queries on two tables on one page

Posted: Tue Aug 19, 2008 9:05 am
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?

Re: two queries on two tables on one page

Posted: Tue Aug 19, 2008 9:42 am
by Wilbo
OK, I was being an idiot, the table I was trying to query had an error in it.

Thanks