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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:48:27+00:00 2026-06-18T10:48:27+00:00

This code prints out a list of column names. But I would expect it

  • 0

This code prints out a list of column names. But I would expect it to print out the information stored in the UserName table for the user with the LoginName of JSmith.

 $loginname = "JSmith";
 $stmt = $dbh->prepare("SELECT * FROM UserName WHERE LoginName=:login_name");
 $stmt->bindValue(":login_name", $login_name, PDO::PARAM_STR);
 $stmt->execute();

echo "<table><tr>";
$result = $stmt->fetch(PDO::FETCH_ASSOC);
 print_r($result);
foreach ($result as $key => $val){
    echo "<th>" . $key . "</th>";   
}
echo "</tr>";
echo "</table>";

Why doesn’t this return the row with the users name? Looking at echo gettype($result) shows me that they are strings and integers; not like each piece of the returned array is itself an array. I’m sure it’s a simple thing, but any help is appreciated. 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-06-18T10:48:28+00:00Added an answer on June 18, 2026 at 10:48 am

    You’re printing the keys, not the values. The keys are the column names, and the values are the corresponding column values.

    print_r($result) shows that $stmt->fetch() returns an array with the information you expect, and the corresponding HTML simply doesn’t reflect that.

    http://php.net/manual/en/pdostatement.fetch.php#example-1018

    Let’s fix it.

    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    print_r($result);
    foreach ($result as $key => $val){
        echo "<th>" . $key . "</th>";
        echo "<td>" . $val . "</td>";
    }
    echo "</tr>";
    echo "</table>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering why this code section prints out the following: print request.user.has_perm('bug_tracking.is_developer'): + str(request.user.has_perm('bug_tracking.is_developer'))
This code is not pretty, but I want to print out the last generated
This code gives me an empty result. I expect it to print out the
This code prints out every entry in the Font Size list combo box in
This code works. it prints out the values in the list ArrayList<String> menuItemsUrl=null; for
I got this code ` // // prints out Hello World! // hello_world(); //First
I'm having a problem with getting this code to print out to file due
This code when executed displays the expected output but prints segmentation fault (core dumped)
I have python code below that will loop through a table and print out
I found out the next issue in this simple code: let () = print_endline

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.