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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:58:58+00:00 2026-06-09T08:58:58+00:00

I’m trying to make the jump from procedural code to the PDO class, and

  • 0

I’m trying to make the jump from procedural code to the PDO class, and have run into problems when querying the DB based on the results of an initial query.

In this instance, I have a menu (id = $parent) of submenus, the details of which I query successfully and are stored in an array for me to access. I then try to form a foreach loop to cycle through that initial array and query the DB again to find the pages belonging to each submenu. It is here the process fails me.

Just as a note, the connection is fine, and the queries also work without a hitch procedurally. I’m merely failing (I think) with the new format.

If someone could point out where I’m going wrong, I’d be grateful. I had been running queries run inside while loops, but I figured foreach was the way to go this time.

The Code

function navigation($parent) {

$conn = ConnManager::get('DB');

    //initial query to get list of submenus belonging to $parent
try {       
    $qNAV= $conn->prepare('SELECT ID, Name FROM prm_menu WHERE Parent = :parent');
    $qNAV->execute(array('parent' => $parent));
    $qNAV->setFetchMode(PDO::FETCH_ASSOC);  
    $n=$qNAV->fetchAll();
} 
catch(PDOException $e) {
    echo $e->getMessage();
}

    //now I try to cycle through the array and run another query each time.
foreach($n as $menu) {
    $id = $menu['ID'];
    try{
        $qPAGE=$conn->prepare('SELECT ID, Title, URLName, MenuOrder FROM posts 
                    WHERE MenuID = $id AND Status = 1 ORDER BY MenuOrder ASC, ID ASC');
        $qPAGE->execute(); 
        $qPAGE->setFetchMode(PDO::FETCH_ASSOC);  
        while($m= $qPAGE->fetchAll()) {
            $menu_item = '<li id="navPage'.$m['ID'].'" class="menu-nav">';
            $menu_item.= '<a href="'.$m['URLName'].'">'.$m['Title'].'</a>';
            $menu_item.= '</li>';

            echo '<div class="menu-nav-wrap">';
            echo '<span class="menu-title">'.$menu['Name'].'</span>';
            echo '<ul class="menu-list">';
            echo $menu_item;
            echo '</ul>';
            echo '</div>';
        }

    }
    catch(PDOException $e) {
        echo $e -> getMessage();
    }
}
}
  • 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-09T08:59:00+00:00Added an answer on June 9, 2026 at 8:59 am

    This is not what you want to do:

    while($m= $qPAGE->fetchAll()) {
    

    Since fetchAll() will return all of the columns, the while statement isn’t iterating over them. What you’re looking for is this:

    $subrows = $qPAGE->fetchAll();
    foreach( $subrows as $m) {
        ....
    

    Also, you should look into SQL JOIN statements, as you may be able to consolidate these nested queries into a single one.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article

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.