Home  >  Article  >  Backend Development  >  大家帮小弟我看看这段代码如何取不到根分类的ID!

大家帮小弟我看看这段代码如何取不到根分类的ID!

WBOY
WBOYOriginal
2016-06-13 13:33:30723browse

大家帮我看看这段代码怎么取不到根分类的ID!急!
/*
 * 根据子类ID获取根分类
 */
function get_root_class($classId) {
global $db, $lot_array;
$class = $db->fetch_first("SELECT pid, name FROM new_infor_class WHERE id = $classId");
if ($class['pid'] != 0) {
get_root_class($class['pid']);
} else {
return $class['name'];
}
}
分类是按照无限分类设置,这样返回的就是$classId的分类名,实在不明白呀!哪位大哥救救我!

------解决方案--------------------
好的,就搭一个测试环境

SQL code
--
-- 表的结构 `tree`
--

CREATE TABLE IF NOT EXISTS `tree` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `pid` int(11) DEFAULT NULL,
  `name` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- 转存表中的数据 `tree`
--

INSERT INTO `tree` (`id`, `pid`, `name`) VALUES
(1, 0, 'a'),
(2, 1, 'b'),
(3, 2, 'c'),
(4, 3, 'd'); <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn