Jump to content


php mysql ->get column metadata pls help

  • Please log in to reply
3 replies to this topic

#1 vanderlay

  • Members
  • PipPip
  • Member
  • 23 posts

Posted 22 May 2006 - 01:01 PM

Hi all,

can someone help me with getting the default value and the correct data type from a mysql table. According to the php manual you use the mysql_fetch_field() call and then reference

meta->def for default
meta->type for type of field

I am using the code below but can not get a default value to list and for type only blob/text etc comes up, i would like to get a detailed typ such as varchar(xx) etc.

Thanks for your advice

/* get column metadata */
    $i = 0;
    while ($i < mysql_num_fields($result)) 
       $meta = mysql_fetch_field($result, $i);
       $flags = mysql_field_flags($result, $i);
       $def = mysql_list_fields($result, $i);
       $value = mysql_fetch_row($result);
       if (!$meta) 
               echo "<td><font ">No information available</font></td>";
    echo "<td><font ">$meta->name</font></td>";
//    echo "<td><font ">$value[1]</font></td>";
    echo "<td><font ">$meta->type</font></td>";
     if (strstr ($flags, 'auto_increment'))
        echo "<td><b><font ">yes</font></td>";
     if (strstr ($flags, 'unsigned'))
        echo "<td><b><font ">yes</font></td>";
    echo "<td><font ">$meta->def</font></td>";


#2 Houdini

  • Members
  • PipPip
  • Member
  • 17 posts

Posted 22 May 2006 - 02:31 PM

What you are wanting is DESCRIBE from MySQL
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]DESCRIBE tablename[/quote]
We the unwilling, led by the unknowing,
have done so much, for so long, with so little,
that we are now quqlified to do anything, with nothing!

#3 craygo

  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 22 May 2006 - 02:36 PM

I have used this in the past to get field details.

$res = mysql_query('select * from tablename') or die (mysql_error());;
while ($i < mysql_num_fields($res)) {
   echo "Information for column $i:<br />\n";
   $meta = mysql_fetch_field($res, $i);
      echo "<pre>
blob:        $meta->blob
max_length:  $meta->max_length
multiple_key: $meta->multiple_key
name:        $meta->name
not_null:    $meta->not_null
numeric:      $meta->numeric
primary_key:  $meta->primary_key
table:        $meta->table
type:        $meta->type
default:      $meta->def
unique_key:  $meta->unique_key
unsigned:    $meta->unsigned
zerofill:    $meta->zerofill



#4 Pennypacker

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 23 May 2006 - 03:45 AM

[!--quoteo(post=376036:date=May 22 2006, 09:31 AM:name=Houdini)--][div class=\'quotetop\']QUOTE(Houdini @ May 22 2006, 09:31 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
What you are wanting is DESCRIBE from MySQL

thanks Houdini "describe" did work as suggested. why dont they put that in the manual?

I did use the meta->def as you pinted out and as per the php manual, however no joy, returned nothing in the array. what ver of mysql are tyou running I have tried v4 & v5


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users