使用PHP和MySQL创建类别引擎的最佳方法

使用PHP和MySQL创建类别引擎的最佳方法,php,mysql,mariadb,Php,Mysql,Mariadb,我需要用PHP和MySQL/MariaDB创建一个分类引擎 示例: CREATE TABLE `categories` ( `id` int(11) NOT NULL auto_increment, `parent` int(11) NOT NULL, `name` varchar(255) NOT NULL, `icon` varchar(255) NOT NULL, `poster` varchar(255) NOT NULL, `description` varch

我需要用PHP和MySQL/MariaDB创建一个分类引擎

示例:

CREATE TABLE `categories` (
  `id` int(11) NOT NULL auto_increment,
  `parent` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `icon` varchar(255) NOT NULL,
  `poster` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) engine="MyISAM" DEFAULT auto_increment="1" ;
class categories
{
    /**
    *   Fetch all subs( recursive )
    */  
    public function getSubsById ($id)
    {
        $rows = array();
        $list = array();
        $query = mysql_query("SELECT id, name, parent, icon, poster, description FROM categories WHERE parent = '$id' ORDER BY id");
        while ($row = mysql_fetch_array($query)) {
            $row["subs"] = $this->getSubsById($row["id"]);
            $rows[] = $row;
        }
        return $rows;
    }
    /**
    *   Fetch all parents categories
    */
    public function fetchParents ()
    {
        $rows = array();
        $query = mysql_query("SELECT id, name, parent, icon, poster, description FROM categories WHERE parent = 0 ORDER BY id");
        while ($row = mysql_fetch_array($query)) {
            $rows[] = $row;
        }
        return $rows;
    }
}
  • 房屋
    • 房子
  • 汽车
    • 汽车
      • 新的
        • 0英里
          • 丰田
            • 雅利斯
              • 。。。
                • 。。。
                  • 。。。
                    • 。。。
我试着用传统的方式创作,但似乎效率低下

SQL(表):

CREATE TABLE `categories` (
  `id` int(11) NOT NULL auto_increment,
  `parent` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `icon` varchar(255) NOT NULL,
  `poster` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) engine="MyISAM" DEFAULT auto_increment="1" ;
class categories
{
    /**
    *   Fetch all subs( recursive )
    */  
    public function getSubsById ($id)
    {
        $rows = array();
        $list = array();
        $query = mysql_query("SELECT id, name, parent, icon, poster, description FROM categories WHERE parent = '$id' ORDER BY id");
        while ($row = mysql_fetch_array($query)) {
            $row["subs"] = $this->getSubsById($row["id"]);
            $rows[] = $row;
        }
        return $rows;
    }
    /**
    *   Fetch all parents categories
    */
    public function fetchParents ()
    {
        $rows = array();
        $query = mysql_query("SELECT id, name, parent, icon, poster, description FROM categories WHERE parent = 0 ORDER BY id");
        while ($row = mysql_fetch_array($query)) {
            $rows[] = $row;
        }
        return $rows;
    }
}
PHP(使用mysqli、pdo或mysql):

CREATE TABLE `categories` (
  `id` int(11) NOT NULL auto_increment,
  `parent` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `icon` varchar(255) NOT NULL,
  `poster` varchar(255) NOT NULL,
  `description` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) engine="MyISAM" DEFAULT auto_increment="1" ;
class categories
{
    /**
    *   Fetch all subs( recursive )
    */  
    public function getSubsById ($id)
    {
        $rows = array();
        $list = array();
        $query = mysql_query("SELECT id, name, parent, icon, poster, description FROM categories WHERE parent = '$id' ORDER BY id");
        while ($row = mysql_fetch_array($query)) {
            $row["subs"] = $this->getSubsById($row["id"]);
            $rows[] = $row;
        }
        return $rows;
    }
    /**
    *   Fetch all parents categories
    */
    public function fetchParents ()
    {
        $rows = array();
        $query = mysql_query("SELECT id, name, parent, icon, poster, description FROM categories WHERE parent = 0 ORDER BY id");
        while ($row = mysql_fetch_array($query)) {
            $rows[] = $row;
        }
        return $rows;
    }
}
如果你知道一些更有效的方法,请让我知道它是什么

也许我应该考虑改变数据库引擎

…部署APC、Memcache、Cache、Json等是另一个问题…


附言:我想创建一个像易趣这样的在线商店系统类别:)

为什么您认为这样做效率低下,导致您考虑更改数据库引擎?我想创建一个像易趣这样的在线商店系统类别:)看看嵌套的集合模型:。