RE: [PHP-DB] Tables etc .....
| From: | John W. Holmes | Date: | Sat, 12 Apr 2003 02:39:46 +0000 |
| Subject: | RE: [PHP-DB] Tables etc ..... | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-27031@lists.php.net to get a copy of this message | ||
> Does anyone have any example code of how to get column names from a
mysql
> table without knowing how many columns there are? Basically what I
need
> to be able to do is to display the contents of a database without
knowing
> the column names as i'm trying to write something to manage my
databases
> and i'd rather try it myself rather than use phpmyadmin all the time
as
> you don't learn anything that way.
I have some example code I can send you off list, if you want, but it's
not that hard to figure out.
1. Issue query with mysql_query(). You do not need to know how many
columns there are or their name.
2. Use mysql_num_fields() to find out how many columns were returned.
3. Loop through the number returned in #2 with mysql_field_name() to get
the field names and create your "header" row.
4. Loop through each row, fetching it with mysql_fetch_row().
5. You already know how many columns there are, so display $row[0] to
$row[num_fields] from the result of mysql_fetch_row in their own <td>
tags.
That's it. Let me know if you want the example code. It does a little
more than this...
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/