Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Display Dynamic Table Data

Display Dynamic Table Data

Display Dynamic Table Data

I am dynamically creating a table based on an uploaded CSV file, and I'd like allow the user to see this information. My query is simply SELECT * FROM `temp`.  How do I go about displaying the content of this table onto the page, since the number of columns could vary?

RE: Display Dynamic Table Data

You don't know how many rows will be in the created table?  My, that sounds pedagogical...

Anyway, in my code I might:

connect to the database server;
select the appropriate db;
issue the SELECT query;
while (fetching rows from the query into a numerical array)
   foreach (elements from the array)
      print the element's value

Want the best answers? Ask the best questions!  TANSTAAFL!

RE: Display Dynamic Table Data

it's not so much the problem of not knowing how many rows. That's no problem. The problem is I don't know how many columns are in my dynamically created table. I mean, well i could figure it out when it's created, but I'm trying to view the data in the table well after it's been created.

Since there are functions that find how many rows are in a table, that's not the issue i'm facing.

My table is created based on a CSV file uploaded by the user, so it could contain 5 columns or 25 columns.

So besides recording in some other table the number of columns (or fields) in the uploaded file, i'm wondering if there's a way to find that out (as well as the field names) to help me output this data into a table.

RE: Display Dynamic Table Data

an easy way (without a separate sql call) is this


$result = mysql_query("Select * from `temp`");
while ($result = mysql_fetch_array($result)){
  if (!isset($done)){
     $number_columns = count($row);
     $done = true;
  //process the recordset as usual

another way to retrieve column information from a database is


$result = mysql_query ("Show Columns from table");
$num_cols = mysql_num_rows($result);

RE: Display Dynamic Table Data

Would the names of the columns also be somewhere in that query?



column 1 | column 2 | column 3 | column 4
row1data | row1data | row1data | row1data
row2data | row2data | row2data | row2data

RE: Display Dynamic Table Data

column names are available in mysql_fetch_assoc() as the key of each array element.

RE: Display Dynamic Table Data

Excellent!  Thank you so much!!  array_keys() is what I was looking for.

Thanks for all the help.

RE: Display Dynamic Table Data

You could use mysql_fetch_field to get the column names. amongst other information.

Quote (PHP online Manual):


(PHP 3, PHP 4, PHP 5)
mysql_fetch_field -- Get column information from a result and return as an object
object mysql_fetch_field ( resource result [, int field_offset] )

Returns an object containing field information. This function can be used to obtain information about fields in the provided query result.


    The result resource that is being evaluated. This result comes from a call to mysql_query().

    The numerical field offset. If the field offset is not specified, the next field that was not yet retrieved by this function is retrieved. The field_offset starts at 0.
Return Values

Returns an object containing field information. The properties of the object are:


      name - column name

      table - name of the table the column belongs to

      def - default value of the column

      max_length - maximum length of the column

      not_null - 1 if the column cannot be NULL

      primary_key - 1 if the column is a primary key

      unique_key - 1 if the column is a unique key

      multiple_key - 1 if the column is a non-unique key

      numeric - 1 if the column is numeric

      blob - 1 if the column is a BLOB

      type - the type of the column

      unsigned - 1 if the column is unsigned

      zerofill - 1 if the column is zero-filled

Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

RE: Display Dynamic Table Data

wow, that helps too, thanks.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close