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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:35:46+00:00 2026-06-15T03:35:46+00:00

I have a database, called ‘catalog’, and a table, called ‘categories’. The table has

  • 0

I have a database, called ‘catalog’, and a table, called ‘categories’. The table has 3 columns in this order: categoryId, categoryName, parentCategory. I’m trying to grab categoryId and categoryName for each row that has a parentCategory = ‘root’. I thought it was a straightforward query, but I’m apparently doing something wrong, because I keep getting the message–Couldn’t execute query–but no mysql error is being displayed. I’ve posted my code below. Can anyone point me straight?

P.S. I do have values assigned to the $db variables; I just didn’t include those here.

<?php
$connect = mysqli_connect($db_host,$db_user,$db_password,$db_database)
    or die ("Couldn't connect to server: ".mysqli_error());

function display_children($parent) {
    $query = "SELECT categoryId, categoryName FROM `categories` WHERE parentCategory=".$parent;
    $result = mysqli_query($connect,$query)
        or die ("Couldn't execute query: ".mysqli_error());

    echo "<ul>";
    while ($row = mysqli_fetch_assoc($result)) {
          echo "<li>".$row['categoryName']."</li>";
          display_children($row['categoryId']); 
    }
    echo "</ul>";
    mysqli_close($connect);
}

?>

<div class="menu">    
<?php
    /* Menu Write */
    display_children("root");
?>
</div>
  • 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-15T03:35:47+00:00Added an answer on June 15, 2026 at 3:35 am

    The display_children() function doesn’t have access to the $connect variable.

    Try this:

    function display_children($parent) {
        global $connect;
        $query = "SELECT categoryId, categoryName FROM `categories` WHERE parentCategory=".$parent;
        $result = mysqli_query($connect,$query)
            or die ("Couldn't execute query: ".mysqli_error());
    
        echo "<ul>";
        while ($row = mysqli_fetch_assoc($result)) {
              echo "<li>".$row['categoryName']."</li>";
              display_children($row['categoryId']); 
        }
        echo "</ul>";
        mysqli_close($connect);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok... I have a database table called employees.. This has columns called ID, Name,
I have a database table called users This table has two columns (that are
I have a MySQL database table called Participant that looks something like this: (idParticipant)
I have a table on my database called 'Log'. This Log table contains ID,
I have a database table called posts that has 'id, title, slug and content'
I have a MySQL database called bookfeather. It contains 56 tables. Each table has
I have a database table called Warehouse this Warehouse's linked to a Product table
I have a database table called user where I have some columns. One of
My question is this, I have my postgres database called gas_stations and only has
Ok so i have a database table called requests with this structure mysql> desc

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.