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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:47:34+00:00 2026-06-13T06:47:34+00:00

I am using PHP loops to populate an HTML table from a MySQL database.

  • 0

I am using PHP loops to populate an HTML table from a MySQL database. I have used PDO to fetch the results of a SQL query into $result:

$result = $pdo->query($sql);    

(where $sql is a SELECT query)

I am then building an HTML table like this:

if($result !== false) 
        {

            $html_table = '
            <table border="1" cellspacing="0" cellpadding="2">
                <tr>';

            foreach($titles as $title)
            {
            $html_table .= "<th> $title </th>";                          
            }

            $html_table .='</tr> <tr>';

            foreach( $result->fetchAll() as $row ) 
            {
                $html_table .= '<tr>' . "\n";

                foreach( $row as $col ) 
                {
                $html_table .= '<td>' .$col. '</td>';
                }
                $html_table .= '</tr>' . "\n";
            }
        }

The idea being to get each record in $result, define them as an array called $row then loop through each record and build the table that way.

The problem is, the table is coming out incorrectly – I can’t discern a pattern, but several cells are repeated, (identical data appears side by side) sporadically.

Any idea where I am going wrong here? Thank you.

  • 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-13T06:47:35+00:00Added an answer on June 13, 2026 at 6:47 am

    Figured this one out! 🙂

    I’ll post my solution here for others in case it helps.

    The issue is the different ways you can fetchAll in PDO. In my code above I am simply using the default fetchAll()

    Which produces and array of arrays like this:

    Array
    (
        [0] => Array
            (
                [NAME] => pear
                [0] => pear
                [COLOUR] => green
                [1] => green
            )
    
        [1] => Array
            (
                [NAME] => watermelon
                [0] => watermelon
                [COLOUR] => pink
                [1] => pink
            )
    
    )
    

    Notice that for each column name there are TWO entries, one for the name and one for the indexing number (not sure if that’s the right term, but you can see what’s going on).

    For my purposes I needed one entry per column so switch to:

    $result->fetchAll(PDO::FETCH_ASSOC)
    

    Which gives this array:

    Array
    (
        [0] => Array
            (
                [NAME] => pear
    
                [COLOUR] => green
    
            )
    
        [1] => Array
            (
                [NAME] => watermelon
    
                [COLOUR] => pink
    
            )
    
    )
    

    And fixed my issue.

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

Sidebar

Related Questions

I currently am using mysql and php to display 9 random results from my
I am using PHP to populate a database of company records. I have a
I am trying to create a table in MySql using php. My code looks
I have simple table that has about 80 rows, which I populate dynamically using
I have a MySQL Table that looks like this: The SQL to create the
I have developed a script that receives json data from a php script using
I am using a PHP while loop in order to get data from a
Using PHP I echo out table rows in a loop like this: <?php /*
Using for loop in PHP can we have numbers associate with a variable name?
I have a homepage that contains a huge amount of php code to populate

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.