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.
two queries on two tables on one page
Moderator: General Moderators
-
lordofgore
- Forum Newbie
- Posts: 4
- Joined: Tue Aug 19, 2008 7:31 am
Re: two queries on two tables on one page
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?
//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
OK, I was being an idiot, the table I was trying to query had an error in it.
Thanks
Thanks