vanderlay Posted May 22, 2006 Share Posted May 22, 2006 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 referencemeta->def for defaultmeta->type for type of fieldI 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 adviceArt[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] Quote Link to comment https://forums.phpfreaks.com/topic/10178-php-mysql-get-column-metadata-pls-help/ Share on other sites More sharing options...
Houdini Posted May 22, 2006 Share Posted May 22, 2006 What you are wanting is DESCRIBE from MySQL[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]DESCRIBE[i] tablename[/i][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/10178-php-mysql-get-column-metadata-pls-help/#findComment-37951 Share on other sites More sharing options...
craygo Posted May 22, 2006 Share Posted May 22, 2006 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->blobmax_length: $meta->max_lengthmultiple_key: $meta->multiple_keyname: $meta->namenot_null: $meta->not_nullnumeric: $meta->numericprimary_key: $meta->primary_keytable: $meta->tabletype: $meta->typedefault: $meta->defunique_key: $meta->unique_keyunsigned: $meta->unsignedzerofill: $meta->zerofill</pre>";$i++;}?>[/code]Ray?> Quote Link to comment https://forums.phpfreaks.com/topic/10178-php-mysql-get-column-metadata-pls-help/#findComment-37953 Share on other sites More sharing options...
Pennypacker Posted May 23, 2006 Share Posted May 23, 2006 [!--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?craygoI 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 & v5thanksArt Quote Link to comment https://forums.phpfreaks.com/topic/10178-php-mysql-get-column-metadata-pls-help/#findComment-38136 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.