Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:27:58+00:00 2026-06-10T13:27:58+00:00

Hy there, i have some tree-structured data in my sqlite-table and want to delete

  • 0

Hy there,

i have some tree-structured data in my sqlite-table and want to delete it recursively with php. The data looks like this:

Id    ParentId
0     -1
1     0

The function:

/**
 * Delete a category with all it's subcategories
 * 
 * @param {number} id The id of the category which should be deleted
 * @return {number} The number of categories deleted
 * @throws {Exception} Throws an exception with some description text if anything goes wrong
 */
public function deleteCategory($id) {
    static $catCnt = 0;

    Helper::log("deleteCategory: id = $id");

    try {
        // Open database-connection
        $this->openDatabase();

        // Remove all subcategories recursively
        $stmtSubs = $this->_db->prepare("SELECT Id FROM categories WHERE ParentId = :id");
        $stmtSubs->bindParam(":id", $id, PDO::PARAM_INT);
        $stmtSubs->execute();
        while ($row = $stmtSubs->fetch(PDO::FETCH_ASSOC)) {
            Helper::log("deleteCategory: call recursively to delete subcategory with id = " . $row["Id"]);
            $this->deleteCategory($row["Id"]);
        }

        // All subcategories have been removed -> Now it's time to remove the category itself
        $stmt = $this->_db->prepare("DELETE FROM Categories WHERE Id = :id");
        $stmt->bindParam(":id", $id, PDO::PARAM_INT);

        Helper::log("deleteCategory: before execution, id = $id");
        $stmt->execute();

        // The code from here on never gets executed...
        Helper::log("deleteCategory: after execution, id = $id");

        $catCnt += $stmt->rowCount();

        return $catCnt;
    } catch (Exception $ex) {
        throw new Exception("Deletion of category failed:<br>" . $ex->getMessage());
    }
}

If i call this function with parameter 0, i’m getting this output:

31.08.2012 09:39:58: deleteCategory: id = 0
31.08.2012 09:39:58: deleteCategory: call recursively to delete subcategory with id = 1
31.08.2012 09:39:58: deleteCategory: id = 1
31.08.2012 09:39:58: deleteCategory: before execution, id = 1
// Here should be the "after execution"-entry, but instead it "waits" half a minute and starts the whole execution from the beginning...
31.08.2012 09:40:28: deleteCategory: id = 0
31.08.2012 09:40:44: deleteCategory: call recursively to delete subcategory with id = 1
31.08.2012 09:40:44: deleteCategory: id = 1
31.08.2012 09:40:44: deleteCategory: before execution, id = 1

As you can see it executes just fine until it should actually delete the subcategory with id 1. The execute-command does never finish, instead it doesn’t seem to do anything for half a minute and after this it starts the whole recursion from the beginning with id 0.

After the second “try” it just returns with an internal server error (“The server encountered an internal error or misconfiguration and was unable to complete your request…”)

If i call this function with parameter 1 everything works fine because the recursion is never called.

What is happening here?

Thanks,

Mik

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:27:59+00:00Added an answer on June 10, 2026 at 1:27 pm

    Struggled around with this like half a day, decided to post it here, posted it here, had another look at it right after posting, saw the problem:

    The recursive call of “$this->openDatabase();” was the source of all evil…

    Thanks 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that looks like this. There is also an autoincrement field
I know there have been some similar questions to this, but they haven't helped
I think I've just been looking at this too long. I have some data
I have a question about how to present some structured data using HTML. I
Let's say that I have an adt representing some kind of tree structure: data
I'm using Python and I have some data that I want to put into
There have been some similar questions asked regarding Grid views, but none have been
I have some text. Is there is any api which can tell me the
I have some VirtualBox VMs running. Is there any way to programatically get the
I have some localization problems in my webpage. There are basically two problems (that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.