这是我的桌子
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中
发布于 2018-07-23 07:32:33
只需在查询中进行计数,并将parent_id作为输入传递。使用prepared statements。
// ....
$stmt = $db->prepare("SELECT COUNT(*) childrenCount FROM relation WHERE parentID=?");
$parentID = '2'
$sth->execute(array($parentID));
// fetch the result here
$stmt->close();https://stackoverflow.com/questions/51473489
复制相似问题