Jump to content

Archived

This topic is now archived and is closed to further replies.

vanderlay

php mysql ->get column metadata pls help

Recommended Posts

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
Art

[code]
/* 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>";

    
    $i++;
    }
    [/code]

Share this post


Link to post
Share on other sites
What you are wanting is DESCRIBE from MySQL
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]DESCRIBE[i] tablename[/i][/quote]

Share this post


Link to post
Share on other sites
I have used this in the past to get field details.

[code]<?
$i=0;
$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
</pre>";
$i++;
}
?>[/code]

Ray




?>

Share this post


Link to post
Share on other sites
[!--quoteo(post=376036:date=May 22 2006, 09:31 AM:name=Houdini)--][div class=\'quotetop\']QUOTE(Houdini @ May 22 2006, 09:31 AM) [snapback]376036[/snapback][/div][div class=\'quotemain\'][!--quotec--]
What you are wanting is DESCRIBE from MySQL
[/quote]

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

craygo
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

thanks
Art

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.