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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:15:47+00:00 2026-05-18T05:15:47+00:00

My goal is pretty straightforward: I want to generate an HTML table with the

  • 0

My goal is pretty straightforward: I want to generate an HTML table with the headers showing the column names of a MySQL table and the body showing the contents of that MySQL table. I’m trying to make this as efficient as possible using loops – the headers were a piece of cake:

$columns = mysql_query("SHOW COLUMNS FROM my_table;");
while($row = mysql_fetch_array($columns)) {
    echo '<th>'.$row['Field'].'</th>';
}

But I’m having trouble getting the body data, and was hoping someone could help. Here’s how I think this should work:

$query = mysql_query("SELECT * FROM my_table;");
while($row = mysql_fetch_array($query)) {
    echo '<tr>';
    foreach($row as $r) {
        echo '<td>'.$r.'</td>';   
    }
    echo '</tr>';
}

But the code above returns two instances of each value (value1, value1, value2, value2…), and I’m pretty sure that’s because each value in the array actually has two keys (the numerical one and the text one), so it’s printing each one twice. I can’t figure out how to get it to just print each field once. I also tried the code below instead of the foreach loop – this code actually prints each of the table’s 12 fields once, but then prints 12 empty td tags, again because it’s double counting the number of items in the array:

for($i =0; $i < count(array_keys($row)); $i++) {
    echo '<td>'.$row[$i].'</td>';
}

Basically, I just need a function that returns the correct length of the array, preferably using a foreach loop but I’m open to a for lopp too – whatever works! Any ideas?

  • 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-18T05:15:48+00:00Added an answer on May 18, 2026 at 5:15 am

    Just replace mysql_fetch_array with mysql_fetch_assoc or mysql_fetch_row.

    Note that instead of doing a SHOW COLUMNS query, you can use mysql_field_name. You’d have to adapt your code a little, though.

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

Sidebar

Related Questions

I'm running a pretty basic subquery on MySQL 4.0.30 . My goal is to
My question is pretty straightforward: You are an executable file that outputs Access granted
The algorithm of A star search with a specified goal is pretty straightforward. However,
I have a pretty basic script that echos local administrator accounts. My goal is
I think I have a pretty simple goal but cant seem to reach it.
I have an assignment (i think a pretty common one) where the goal is
Goal is to make a dialog that appears on menu_key pressed, but it keeps
Goal: Produce an Excel document with information from 3 associated models that is similar
Goal: to create a percentage column based off the values of calculated columns. Here's
My goal is to allow pretty printing of Qt classes in gdb. I.e if

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.