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 6569197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:35:46+00:00 2026-05-25T14:35:46+00:00

I have a SQLite DB with a table called library containing 3 fields (12php

  • 0

I have a SQLite DB with a table called “library” containing 3 fields (12php sMb)

Id (unique identifier) //
libraryName, containing a string //
parentLibId, containing the ID of the parent Library.

If the library is in the “root”, parentLibId is Null (empty)

I need an array that will return a tree with parents and child starting from the root.

Anyone know how to do that without too many code?

$dbh = new PDO('sqlite:my.sqlite') or die("Error 0xDB0001");
$sth = $dbh->prepare("SELECT * FROM library");
$sth->execute();
$result = $sth->fetchAll();

I have a time processing page limit given by my friend as a little competition and this part I cannot figure it out, I used a code but it’s so long, I feel stupid XD

Any help is welcomed, if I’m unclear, just say it and I’ll give more details.

Thank you very much your help is appreciated!!

Have a good evening all!

  • 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-05-25T14:35:47+00:00Added an answer on May 25, 2026 at 2:35 pm

    this function is not tested. This is main idea.

    getTree();
    
    function getTree($child = null)
    {
        $dbh = new PDO('sqlite:my.sqlite') or die("Error 0xDB0001");
    
        //It's just example. Security and best practice is your problem :p
        if ($child)
            $where = 'parentLibId = ' . $child;
        else
            $where = 'parentLibId IS NULL ';
    
        $sth = $dbh->prepare("SELECT * FROM library" . $where);
        $sth->execute();
    
        if ($child)
        {
            $result = $sth->fetchAll()
            $return = $result;
        }else{ //get parents
            while ($result = $sth->fetch())
            {
                //one by one
                $return[] = array(  'parent' => $result, //parent info
                                    'childs' => getTree($result['id']) //childs
                                );
            }
        }
    
        return $return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQLite table called posts . An example is shown below. I
Okay, so I have a (SQLite) table called log wherein one column (time) is
I have a simple SQLite table called message: sequence INTEGER PRIMARY KEY type TEXT
I created a SQLite database via SQLiteManager. I have a table called Envelopes, and
I have a table called person and it contains two fields, name (TEXT) and
I am creating a SQLite database for my iPhone projects. I have table called
I have an SQLite table that contains a BLOB I need to do a
I have a sqlite (v3) table with this column definition: timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
I have a boolean (BOOL) type field in the SQLite table. In the SubSonic
I have a table in SQLite: CREATE TABLE test_results(timestamp TEXT, npass INTEGER, nfails INTEGER)

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.