首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php计算父节点的所有子节点

如何使用php计算父节点的所有子节点
EN

Stack Overflow用户
提问于 2018-07-23 07:22:23
回答 1查看 219关注 0票数 0

这是我的桌子

代码语言:javascript
复制
parentID   id
1          0
1          2
3          4
2          3
4          5
5          6
6          7
7          8
8          9
9         10

这是我的php代码-->

代码语言:javascript
复制
 function countChildren($parentId) {

     $link=mysql_connect("localhost","root","");
     mysql_select_db("employee",$link);

     $sql = ("select id from relation where parentID='2'");
     $result=mysql_query($sql,$link);
     $count = mysql_num_rows($result);

     $userinfo = array();

     while ($row_user = mysql_fetch_assoc($result))
    {
       $userinfo[] = $row_user;
    }
    foreach($userinfo  as $userId) 
    {
       $count += countChildren($userId);
    }
return $count;

}

现在,我想使用上面的代码来计数父节点的所有子节点,但是我的代码出现了错误,例如-->

致命错误:达到“100”的最大功能嵌套级别,中止!在第7行的C:\wamp\www\Project\index.php中

EN

回答 1

Stack Overflow用户

发布于 2018-07-23 07:32:33

只需在查询中进行计数,并将parent_id作为输入传递。使用prepared statements

代码语言:javascript
复制
// ....

$stmt = $db->prepare("SELECT COUNT(*) childrenCount FROM relation WHERE parentID=?");
$parentID = '2'
$sth->execute(array($parentID));

// fetch the result here

$stmt->close();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51473489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档