PHP recursive function to delete all child nodes causes stackoverflow

前端 未结 2 1376
深忆病人
深忆病人 2021-02-15 18:47

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

\'id\', \'content\', \'parent\'

where:

  • id = the id of the category<
2条回答
  •  情书的邮戳
    2021-02-15 19:28

    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.

提交回复
热议问题