My MySQL looks like this: (the name of the table is category)

我的MySQL看起来像这样:(表的名称是类别)

'id', 'content', 'parent'

where:

  • id = the id of the category
  • id =类别的ID

  • content = some-text-we-dont-care-about
  • content = some-text-we-dont-care-about

  • parent = the id of the parent category
  • parent =父类别的ID

this is what I'm trying right now:

这就是我现在正在尝试的:

function remrecurs($id) {
    $qlist=mysql_query("SELECT * FROM category WHERE parent='$id'");
    if (mysql_num_rows($qlist)>0) {
         while($curitem=mysql_fetch_array($qlist)) {
              remrecurs($curitem['parent']);
         }
    }
    mysql_query("DELETE FROM category WHERE id='$id'");
}

Which for some reason doesnt work and crashes .. Any idea what I'm doing wrong ?

由于某种原因,它不工作和崩溃..任何想法我做错了什么?

2 个解决方案

#1


11

The problem is in the recursive call:

问题出在递归调用中:

remrecurs($curitem['parent']);

it should be:

它应该是:

remrecurs($curitem['id']);

Why?

Your objective is to delete the row with given id. First you check to see if it has any children. If yes you need to call the recursive delete on each of the children not on the parent again. You are calling the function recursively on the parent again..this leads to infinite recursive calls, you thrash the stack and crash.

您的目标是删除具有给定ID的行。首先你检查它是否有孩子。如果是,则需要再次对不在父项上的每个子项调用递归删除。您再次在父级上递归调用该函数。这会导致无限递归调用,您会崩溃堆栈并崩溃。

更多相关文章

  1. PHP按照 年、月、日创建递归目录
  2. php 无限级分类 递归+sort排序 和 非递归
  3. 如何在产品和类别应用程序树中将1个表连接到(2个不同的表作为一个
  4. 使用python api递归计算每个Dropbox文件夹大小
  5. 学习python的第十六天(迭代器,三元表达式,列表生成式,字典生成式,
  6. 绕脑的汉诺塔递归
  7. sqlserver 存储过程 递归查询分组+hierarchyid重建会员关系
  8. SQL Server 2005递归查询在数据中有循环,这是可能的吗?
  9. 如何在Java中递归解压缩文件?

随机推荐

  1. Android中对同一个TextView设置不同字体
  2. 用 Golang 开发 Android 应用(六)—— Came
  3. 【10.0.1】ArcGIS Runtime for Android之
  4. android用户界面之Widget教程实例汇总
  5. android 局域网聊天工具(可发送文字/语音
  6. Android 内核简单分析
  7. 在mac 上配置AndroidStudio碰到的坑
  8. android HAL介绍
  9. Android(安卓)创建与解析XML(二)—— Dom方
  10. Android:Android学习路线图