Forum Moderators: open

Message Too Old, No Replies

Table field exists?

         

kkonline

4:09 am on Oct 28, 2007 (gmt 0)

10+ Year Member



Is it possible to know whether a particular table field exista in a table or not?

Secondly is it possible to display all the table fields existing in the table.

I have a table called articles inside a database named mysql
I want to display all the table fields present in articles. How to do?

rocknbil

9:56 am on Oct 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wouldn't use the database mysql for your projects. mysql is the internal mysql database for user names, accounts, etc. Create a new database for each project.

Get field names with this command:

describe [tablename];

In your programming, you can use describe to strip off the other table info from describe table (if it's present, depends on your programming) and stuff the field names into an array. A perl example, using the stock DBI module:

@field_list = &table_fields('table_name');


sub table_fields {
my ($sth,$rv,@r,@fields,$table);
$table = shift(@_);
if ($table eq '') { die("No table provided"); }
$sth = $dbh->prepare("describe $table;");
$rv = $sth->execute or &die("Cannot describe table $table");
while ((@r) = $sth->fetchrow_array) { push (@fields, $r[0]); }
$sth->finish;
return @fields;
}