I want to display menu from mysql database. This is what I tried so far and don't know what is the error for. I appreciate any help . I posted the table that I want to display and the error that I got
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\\wamp\\www\\second _try\\eCRA.php on line 18
<?php
function display_menu($parent, $level) {
$query=mysql_query("SELECT n.id, n.menu_name, n.link, d.count FROM 'menu' n
LEFT OUTER JOIN(SELECT parent, COUNT (*) AS count FROM 'menu' GROUP BY
parent) d ON n.id=d.parent W HERE n.parent=".$parent);
echo"<ul>";
while($row = mysql_fetch_assoc($query)) {
if($row['count']>0) {
echo"<li><a href='" .$row['link']. "'>". $row['menu_name'] ."</a>";
display_menu($row['id'], $level + 1);
echo"</li>";
}//f
elseif($row['count']==0) {
echo"<li><a href='". $row['link'] ."'>". $row['menu_name'] ."</a></li>";
}else;
}//w
echo"</ul>";
}
display_menu(0,2);
?>
![error][1]
![Table][2]
On line 11
you have $row['count']
which means you are retrieving the column count
which is not in in your database columns (As per your attached image), I think you want this.
if(mysql_num_rows($query)>0){
//do stuff here
}
elseif(mysql_num_rows($query)>==0){
//do stuff here
}
Here's what's wrong:
$query=mysql_query("SELECT n.id, n.menu_name, n.link, d.count FROM 'menu' n
LEFT OUTER JOIN(SELECT parent, COUNT (*) AS count FROM 'menu' GROUP BY
parent) d ON n.id=d.parent W HERE n.parent=".$parent);
You're using single quotes for referencing to a table name. You cannot do that. You'd need to use backticks for that!
Like so:
$query=mysql_query("SELECT n.id, n.menu_name, n.link, d.count FROM `menu` n
LEFT OUTER JOIN (SELECT parent, COUNT (*) AS count FROM `menu` GROUP BY
parent) d ON n.id=d.parent W HERE n.parent=".$parent);
I would also like to point out that your code is possibly vulnerable to SQL injection. Look into that, if you don't mind.
You're using the wrong syntax. FROM 'menu'
should be with backticks instead of single quotes:
FROM `menu`
mysql_query fails to get the result use mysql_error() to find the problem
function display_menu($parent, $level) {
$query=mysql_query("SELECT n.id, n.menu_name, n.link, d.count FROM 'menu' n
LEFT OUTER JOIN(SELECT parent, COUNT (*) AS count FROM 'menu' GROUP BY
parent) d ON n.id=d.parent W HERE n.parent=".$parent) or die(mysql_error());
echo"<ul>";
while($row = mysql_fetch_assoc($query)) {
if($row['count']>0) {
echo"<li><a href='" .$row['link']. "'>". $row['menu_name'] ."</a>";
display_menu($row['id'], $level + 1);
echo"</li>";
}//f
elseif($row['count']==0) {
echo"<li><a href='". $row['link'] ."'>". $row['menu_name'] ."</a></li>";
}else;
}//w
echo"</ul>";
}
display_menu(0,2);
Assuming parent is numeric, try this...
SELECT n.id
, n.menu_name
, n.link
, d.count
FROM menu n
LEFT
JOIN
( SELECT parent
, COUNT (*) count
FROM menu
GROUP
BY parent
) d
ON n.id = d.parent
WHERE n.parent = $parent;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.