超过俩小时未解决的递归死循环。。求救
private function countAllChild($tempCount, $mid, $level = 0)
{
// die('死循环阻止');
$tableShopMember = 'ewei_shop_member';
// 根据uid拿到shopmember对象
logging_run($mid . '查找等级:' . $level);
if ($level > 0) {
if ($mid == null) {
return;
}
$sql = 'select id from ' . tablename($tableShopMember) . ' where agentlevel >= ' . $level . ' and agentid = ' . $mid;
$childrenDirect = pdo_fetchall($sql);
} else {
$childrenDirect = pdo_getall($tableShopMember, ['agentid' => $mid], array('id', 'uid'));// 直接下级
}
$childrenDirect ? array_push($this->childrenArray, $childrenDirect) : '';
$countCurr = count($childrenDirect);
if ($countCurr > 0) {
foreach ($childrenDirect as $l) {
$agentId = $l['id'];
// logging_run('传入countAllChild' . $agentId);
$sql = 'select id from ' . tablename($tableShopMember) . ' where agentlevel >= ' . $level . ' and agentid = ' . $agentId;
$childrenDirect1 = pdo_fetchall($sql);
// var_dump($childrenDirect1);
$childrenDirect1 ? array_push($this->childrenArray, $childrenDirect1) : '';
$countCurr1 = count($childrenDirect1);
logging_run($agentId . '的下级数量=' . $countCurr1);
// logging_run($childrenDirect1);
if ($countCurr1 > 0) {
$this->countAllChild($tempCount, $agentId, $level);
}
}
}
}
打扰了。。程序确实没有导致死循环的地方,是因为我数据的上下级关系形成了闭环。。
在循环中查询数据库,会出现什么问题?分分秒秒是数据库连接超出限制,可能是几何级别次方的事情。
@sethhu 可是查询多个人的下级 除了用循环还有什么好方案,求指导。
只有两级不是无限下级,可以优化一下循环查询数据那块,即使是无限循环可以一次取出数据在做数据处理
同楼上 先一次性从数据库查出相关数据 ,然后在做循坏处理。
还有你这样是一次性返回完整的上下级关系吗?
如果不是你可分步查找。相当于先查一级,然后用户点击二级,在请求接口查询二级。