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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:00:50+00:00 2026-05-27T05:00:50+00:00

I have a simple mysqli wrapper class I use for my database operations. I

  • 0

I have a simple mysqli wrapper class I use for my database operations. I am defining (instantiating?) that class at the top of the code presumably where it should be globally accessable, however when I try to use this reference to the db class within a recursive function, xdebug tells me it is out of scope – so as a fix I have had to define the database twice, but this seems like poor practice. Can anyone tell whats up or where I’m going wrong?

The code is recursively printing nested comments from the database FYI.

The code is as follows…

<?php
require 'lib/mysqli.class.php';  // the pretty standard mysqli class
$config = array();$config['host'] = 'localhost';$config['user'] = 'root';$config['pass'] = '';$config['table'] = 'publicate';

$db = new DB($config);     // new instance of database
//$db->setFetchMode(2);      // fetch data by association (MYSQLI_ASSOC)

// Run a Query:
$db->query('SELECT * FROM comments WHERE parentid = 0');        

// Get the data:
$root_sql = $db->get();

recursive_categories($root_sql);

function recursive_categories($results)
{
     if(count($results))
     {
         echo "<ul>";
         foreach($results as $res)
         {
             echo "<li>" . "id=" . $res['id'] . ", pid=" . $res['parentid'] . ", content: " . $res['content'];


             //Rest of what ever you want to do with each row

             //Check this category for children ************************
             //2nd definition of DB ************************
             $config = array();$config['host'] = 'localhost';$config['user'] = 'root';$config['pass'] = '';$config['table'] = 'publicate';
             $db2 = new DB($config);     // new instance of database
             $db2->query("SELECT * FROM comments WHERE parentid  = " . $res['id']);
             $rows = $db2->get();

             recursive_categories($rows);

             //has to be after the inner loops
             echo "</li>";
         }
         echo "</ul>";
     }
}
?>

Thanks.

  • 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-27T05:00:51+00:00Added an answer on May 27, 2026 at 5:00 am

    You need to pass your $db connection to the function like so:

    function recursive_categories($results, $db)
    

    Then it will be available inside the function variable scope.

    Another issue that comes to mind … If this file resides inside a publicly accessible web directory you definitely don’t want to have your actual database credentials just chilling there out in the open.

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

Sidebar

Related Questions

I'm writing a semi-simple database wrapper class and want to have a fetching method
i have simple singleton that sets pdo object from factory class , in the
I have the following simple mysqli php application, which should work fine. $pk is
I have a simple core class that is being used for core functions to
I have a simple MySQL table that is setup for full text search. |
I created a simple MySQLi class for a project I'm working on to make
I have a simple mysql table I want to use for daily stat tracking.
I have a few classes, such as those that outline database table structure or
I have this code, and works perfectly, but i want to make a simple
We have a custom software project that's database driven (MySQL) and is built with

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.