Why is this MySQL Join Statement not Working?
Here is my code:
$query2 = mysql_query("SELECT * FROM categories WHERE parent = $id JOIN SELECT * FROM posts WHERE main_nav_page = '$idTwo'");
while ($row2 = mysql_fetch_assoc($query2)) {
$id = $row2['id'];
开发者_如何学编程 $name = $row2['name'];
$slug = $row2['slug'];
$subMenuOrder = $row2['sub_menu_order'];
echo "<tr>\n";
echo "<td> -- $name</td>\n";
echo "</tr>\n";
}
Is my syntax wrong?
EDIT:
the error message is:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/studentw/public_html/new_nav.php on line 30
Instead of that you probably want something more like:
SELECT *
FROM
categories c
INNER JOIN
posts p ON c.categoryid = p.categoryid
WHERE
c.parent = $id
AND p.main_nav_page = '$idTwo';
Note that the tables are joined, rather than select statements. Also, joins are specified in the FROM
clause.
Try this:
$results = mysql_query("query here") or die(mysql_error());
I think there is a small syntax error. Looks like you missed the single quotes (' ') around "$id" in the WHERE parent
-clause. Should be like this, I'm guessing:
SELECT * FROM categories WHERE parent = '$id' ...
精彩评论