Php 是否可以在动态菜单的单个查询中获取类别子目录子类别?

Php 是否可以在动态菜单的单个查询中获取类别子目录子类别?,php,html,css,mysql,Php,Html,Css,Mysql,我有两个分类表和子分类表 Create table m_category (id int, cat_name varchar(30), constraint pk_catid primary key (id) ); Create table m_item (id int, cat_id int, item_name varchar(30), constraint fk_ca

我有两个分类表和子分类表

       Create table m_category (id int, 
       cat_name varchar(30), 
       constraint pk_catid primary key (id)
       );

       Create table m_item (id int, 
       cat_id int, 
       item_name varchar(30), 
       constraint fk_catid 
       foreign key (cat_id)
       references m_category(id)
       );
我想让我的菜单看起来像。 我希望你明白了。 我的想法是运行如下查询

$sql="SELECT * FROM m_category where id='1';
并使用这些查询同样获取每个类别和子类别

但是我想让菜单完全动态化,并在不到3个查询中获取类别和子类别

可以这样做吗?

您可以使用连接

  $sql="SELECT * 
        FROM m_category 
        INNER JOIN m_item on m_category.id = m_item.fk_catid
        where id='1';";
使用PDO
假设$dbh是数据库连接句柄(如果使用mysqli,则有类似的功能)


但我希望查询是完全动态的,也就是说,我不必将id定义为等于1,您可以使用var来动态分配您的父id。。sintax取决于您使用的mysql驱动程序,这意味着什么不起作用。。你有错误吗?显示错误消息。。错误的结果。。显示结果和预期结果。?显示正确的数据样本和预期结果。。(文本不是img)
$id = 1;

$sth = $dbh->prepare("SSELECT * 
            FROM m_category 
            INNER JOIN m_item on m_category.id = m_item.fk_catid
            where id= :id");
$sth->bindParam(':id', $id, PDO::PARAM_INT);