Welcome to WebmasterWorld Guest from

Forum Moderators: incrediBILL

Message Too Old, No Replies

Displaying a MySQL table?



3:33 pm on Mar 14, 2005 (gmt 0)

10+ Year Member

Forgive my ignorance if this is in the wrong place, I didn't see a forum for it.

I used $query="ALTER TABLE news ADD align1 varchar(10) NOT NULL";

To add three columns. But they are not working; the values are always empty.

I tried using SHOW to dump the table.

All I want is to see the column names in a table.

But I can't figgure out how to hash the result and display it, without already KNOWING whats in there?

Please help?


4:04 pm on Mar 14, 2005 (gmt 0)

10+ Year Member

You cannot enforce a "not null" on a new column in a table that already has rows.

$show = "SHOW COLUMNS FROM news";

should work.

From MySQL's site:

SHOW [FULL] COLUMNS FROM tbl_name [FROM db_name] [LIKE 'pattern']

SHOW COLUMNS lists the columns in a given table. If the column types differ from what you expect them to be based on your CREATE TABLE statement, note that MySQL sometimes changes column types when you create or alter a table. The conditions for which this occurs are described in Section, “Silent Column Specification Changes”.

The FULL keyword can be used from MySQL 3.23.32 on. It causes the output to include the privileges you have for each column. As of MySQL 4.1, FULL also causes any per-column comments to be displayed.

You can use db_name.tbl_name as an alternative to the tbl_name FROM db_name syntax. These two statements are equivalent:

mysql> SHOW COLUMNS FROM mytable FROM mydb;
mysql> SHOW COLUMNS FROM mydb.mytable;

SHOW FIELDS is a synonym for SHOW COLUMNS. You can also list a table's columns with the mysqlshow db_name tbl_name command.

The DESCRIBE statement provides information similar to SHOW COLUMNS. See Section 13.3.1, “DESCRIBE Syntax (Get Information About Columns)”.


4:24 pm on Mar 14, 2005 (gmt 0)

10+ Year Member

This is what I did.

$query = "SHOW COLUMNS FROM news";


print "There are $num_results columns.<br>";
$row = mysql_fetch_array($result);

for ($i = 0; $i < $num_results; $i++)

print "Field " . $row[$i] . " exists.<br>";

And this is the results. It did not give me the names of the columns :( But rather, something else.

There are 11 columns.
Field id exists.
Field int(6) exists.
Field exists.
Field PRI exists.
Field exists.
Field auto_increment exists.
Field exists.
Field exists.
Field exists.
Field exists.
Field exists.


4:30 pm on Mar 14, 2005 (gmt 0)

10+ Year Member

check the "desc" command.

desc [table name] (e.g. desc invoice)


4:53 pm on Mar 14, 2005 (gmt 0)

10+ Year Member


I got it with the DESC command. The new columns are there.

So why does it not work?

I can set the value for the column. Value is correct type.

Then when I read the value, its blank. Like it never set.

When you ADD columns, does it add them for already existing entries? Maybe it does not, and thats why it wont take?


Featured Threads

Hot Threads This Week

Hot Threads This Month