Re: Possible to get field names and types in a table without executing a query?





brian.barto@xxxxxxxxxxxxxxxxxxx wrote:
Hi all. My goal is to get a list of all field names and data types for
those fields in any given table in a mysql database. From reading the
DBI documentation, the only way I've been able to do this is by
preparing and executing a query against a table first. Then I would use


Instead of trying to hack together the data bases on an assumed behavior and pseodo query, do a query that actually actually asks for what you want.

For instance with MySQL:

print Dumper $dbh->select_all_arrayref('SHOW TABLE STATUS FROM "db" LIKE "table"');

# same as SHOW COLUMNS FROM db.table
print Dumper $dbh->select_all_arrayref('DESCRIBE db.table');

HTH :)

$sth->{NAME} and $sth->{TYPE} to get the field names and their
respective data types. it looks like this:
$sth = $dbh->prepare("SELECT * FROM $table") or die "Can't prepare
statement!\n";
$sth->execute or die "Can not execute statement!\n";
@types = @{$sth->{TYPE}};
@cols = @{$sth->{NAME}};

PS: use strict and warnings or you're going to have headaches when reusing $sth @types, @cols, etc...
.



Relevant Pages

  • Re: Possible to get field names and types in a table without executing a query?
    ... $FIELDLENGTHS} and type $FIELDTY ... those fields in any given table in a mysql database. ... preparing and executing a query against a table first. ... Instead of trying to hack together the data bases on an assumed ...
    (perl.dbi.users)
  • Re: function does not return
    ... Some servers will put slashes on returning data, ... The exception to this is if you called addslashes() before you inserted the data in the MySQL database. ... The ifstatement probably made sense years ago, before the query was assigned the line before. ...
    (alt.php)
  • Re: How do mysql_fetch_assoc/mysql_query work?
    ... When I submit a query using PHP I ... at least the data I want) in an array. ... I submit a query to a MySQL database it goes away, ... What language are you using? ...
    (comp.lang.php)
  • Re: How do mysql_fetch_assoc/mysql_query work?
    ... Jerry Stuckle wrote: ... When I submit a query using PHP I ... Maybe you don't want a 2D array? ... I submit a query to a MySQL database it goes away, ...
    (comp.lang.php)
  • Getting this week from database?
    ... Dear Folks and Friends, ... I screwed up somewhere and had this code working before, but now, ... I have a simple MySql database which has ... the query returns no data at all. ...
    (alt.php)