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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:24:27+00:00 2026-05-16T11:24:27+00:00

I have a pdo function that fetches usernames and user ids from the database.

  • 0

I have a pdo function that fetches usernames and user ids from the database. When I run the function i get different results.

print_r gives

Array ( [ID] => 58 [username] => abdullatif )

and foreach gives me

5-5a-a

There is one row that matches the pdo query in the database.

public function getUserCredentials($userName, $password){
$this->db   = new Dbpdo_Database(); 
$this->db->connect();
try{  
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $dbh->prepare("SELECT 
            ID 
          FROM 
            administrators 
          WHERE 
            username = :username 
          AND 
            user_password = :password
          LIMIT 1");  


    $stmt->bindParam(':username', $userName, PDO::PARAM_STR);
    $stmt->bindParam(':password', $password, PDO::PARAM_STR);
    }

    $stmt->execute();

    /*** fetch the results ***/
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    return $results;

    /*** close the database connection ***/
    //$dbh = null;
}
catch(PDOException $e){
    echo $e->getMessage();
  } 
}

$results = $mydb->getUserCredentials($userName, $password);

foreach ($results as $row){
echo $row['ID'].'-'.$row['username'];
}

print_r($results);

Any hints as to whats wrong would be much appreciated. Thanks in advance.

  • 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-16T11:24:28+00:00Added an answer on May 16, 2026 at 11:24 am

    Your $results array is one-dimensional – it has an ID and a username. Your foreach is looking for a two-dimensional array, with each row containing an ID and a username. Change it to:

    foreach($results as $key => $value) {
        echo $key . '-' . $value;
    }
    

    and you should get:

    ID-58
    username-abdullatif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What happens when two different clients call the same php function that have pdo::beginTransaction?
I have a function that retrieves data from a Mysql database, stores the values
I have an issue with PDO that I'd really like to get an answer
I'm using PDO inside a database abstraction library function query that I've made. I'm
i have simple singleton that sets pdo object from factory class , in the
I have a simple recursive array function that looks like this: function recursive_array($results) {
I have a database class which user can instantiate. class Foo extends PDO {
i have a class that extends the PDO class. it's called Database. but in
I decided to have a go at PDO. The function below should move a
I have a PDO Query that returns what appears to be an array $pds.

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.