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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:57:38+00:00 2026-05-30T23:57:38+00:00

$db = new PDO(mysql:host=$hostname;dbname=$database, $username, $password); $items = ‘SELECT items FROM menus’; $itemLink =

  • 0
$db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);

            $items = 'SELECT items FROM menus';
            $itemLink = 'SELECT itemLink FROM menus';

            $itemQuery = $db->query($items);
            $linkQuery = $db->query($itemLink);

            $fetchItem = $itemQuery->fetch(PDO::FETCH_ASSOC);
            $fetchLink = $linkQuery->fetch(PDO::FETCH_ASSOC);

            $merged = array_merge($fetchItem,$fetchLink);

            foreach($merged as $key=>$value){
                echo "${key} =>  ${value} <br />";
            }

This is what it looks like in the database:

items   |itemLink
----------------------
Kill Bill|Kill Bill link
Preman  |Preman link

So, the expected output, or at least what I thought must be this:

    items => Kill Bill
    items => Preman
    itemLink => Kill Bill Link 
    itemLink => Preman Link

But the resulted output from the code is this:

items => Kill Bill
itemLink => Kill Bill Link 

It’s missing the other items and itemLink

So, how do I achieve the output that I want?

  • 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-30T23:57:40+00:00Added an answer on May 30, 2026 at 11:57 pm
            $fetchItem = $itemQuery->fetch(PDO::FETCH_ASSOC);
            $fetchLink = $linkQuery->fetch(PDO::FETCH_ASSOC);
    

    This only fetches the first row of each resultset. You need fetchAll:

            $fetchItem = $itemQuery->fetchAll(PDO::FETCH_ASSOC);
            $fetchLink = $linkQuery->fetchAll(PDO::FETCH_ASSOC);
    

    and adjust the rest of your code.

            foreach($merged as $entry) {
              foreach( $entry as $key => $value ) {
                echo "${key} =>  ${value} <br />";
              }
            }
    

    EDIT:
    The call of fetch only retrieved the first row of the resultset, whereas fetchAll parses the complete resultset into an Array. So the Objects look like this afterwards:

    Array(
      [0] => { 'items' => 'Kill Bill' },
      [1] => { 'items' => 'Preman' }
    )
    Array(
      [0] => { 'itemLink' => 'Kill Bill' },
      [1] => { 'itemLink' => 'Preman' }
    )
    

    array_merge concatenate both arrays to the following:

    Array(
      [0] => { 'items' => 'Kill Bill' },
      [1] => { 'items' => 'Preman' },
      [2] => { 'itemLink' => 'Kill Bill' },
      [3] => { 'itemLink' => 'Preman' }
    )
    

    So we now have a two dimensional array. To traverse the values we need first to select each $entry, which is done in the outer foreach and can afterwards access the key/value structure in the inner foreach.

    As pointed out in the other comment: If you want to preserve the connection between itemsand itemLink, you should change the query in the first place to

    SELECT items, itemLink FROM menus
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

try { $dbh = new PDO('mysql:host=localhost;dbname=wqposts', 'root', ''); $query = SELECT wqpid FROM threads
I have the following code: $link = new PDO(mysql:dbname=$databasename;host=127.0.0.1,$username,$password); $query = $link->prepare(SELECT * FROM
I have a class with this in self::$DB = new PDO(mysql:dbname=$dbname;host:=127.0.0.1 , 'root' ,
Possible Duplicate: declare property as object? class core { public $dbh = new PDO(mysql:dbname=newdbnaem;host=1.1.1.1:1111,
I am trying to instantiate a PDO object like this: $this->pdo['pdo'] = new PDO('mysql:host=127.0.0.1;dbname=mydb;charset=UTF-8',
I'm using this code and I'm beyond frustration: try { $dbh = new PDO('mysql:dbname='
while using the following constructor $this->pdo = new PDO ($this->source, $this->username, $this->password); if I
I'm just trying to do a simple insert: $dbh = new PDO(mysql:host= . WEBSITE_SERVER
I want to select a MySQL database to use after a PHP PDO object
I am using PDO with PHP to create a new database and then a

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.