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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:04:01+00:00 2026-06-12T03:04:01+00:00

I have a simple table with one field id, and when I execute this

  • 0

I have a simple table with one field “id”, and when I execute this code…

$dbh = new PDO('mysql:host='.$dbhost.';dbname='.$dbname, $dbuser, $dbpass);

$sql = 'SELECT * FROM logolist';
$q = $dbh->query($sql);
while($r = $q->fetch()){ print_r($r); }

… I get this output:

Array
(
    [ID] => 2
    [0] => 2
)
Array
(
    [ID] => 4
    [0] => 4
)

As you see, there’s a [0] under the field “ID”. if I add more field, I keep getting more extra elements inside the array. It’s like every field is outputting it’s value 2 times.

Why is this?

  • 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-12T03:04:03+00:00Added an answer on June 12, 2026 at 3:04 am

    I’m encountering this practice of having a loop for fetching MySQL results and I’m wondering why people do it so I’ll write up this answer and try to clear up a few things.

    1) You do not need a loop to fetch results
    2) Reason you get the results duplicated is because you’re receiving an associative array and index-based one. That’s the default behaviour.

    What you can do is this:

    $dbh = new PDO('mysql:host='.$dbhost.';dbname='.$dbname, $dbuser, $dbpass);
    
    // Tell PDO to throw exceptions in case of a query error
    $dbh ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    try
    {
        $result = $dbh->query("SELECT * FROM logolist")->fetchAll(PDO::FETCH_ASSOC); 
    
        // Your result is now in $result array which is associative. 
        // If it's empty - no results were found. 
        // In case of an error, you'd go to catch block of the code and you'd echo out an error. 
    }
    catch(PDOException $e)
    {
        echo "Error reported: ". $e->getMessage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this time i have a simple question: I have one table, related with himself
I have a MySQL table like this one: day int(11) hour int(11) amount int(11)
I have a simple table where one of the field is a date column.
I have simple database - one table with 6 collumns. 3 of them i
I have a simple table with autoincrementing ID's and one text column. id(int), value(nvarchar)
I have a simple map/reduce job that scans one hbase table, and modifies another
I have a LINQ to SQL class VoucherRecord based on a simple table. One
I have a simple page. One Rich Text Box bound to a test table
I got a simple problem in SQLAlchemy. I have one model in a table,
I have a simple form. Its got one field and a submit button. I

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.