簡體   English   中英

如何使用php計算父節點的所有子節點

[英]how to count all the children node of a parent node using php

這是我的桌子

parentID   id
1          0
1          2
3          4
2          3
4          5
5          6
6          7
7          8
8          9
9         10

這是我的PHP代碼->

 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中

只需在查詢中進行計數並傳遞parent_id作為輸入即可。 使用准備好的陳述

// ....

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

// fetch the result here

$stmt->close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM