note 20082 added to function.mysql-fetch-field

From: Date: Thu, 21 Mar 2002 21:09:30 +0000
Subject: note 20082 added to function.mysql-fetch-field
Groups: php.notes 
Request: Send a blank email to php-notes+get-28046@lists.php.net to get a copy of this message
I needed to get the field information and the enum/set values. Here is the function I created to expand the object returned by mysql_fetch_field. I also, decided to return all the fields for a table in an array of field objects by "name" and position much like mysql_fetch_array does. You could test it by using: $myfields = GetFieldInfo('test_table'); print "<pre>"; print_r($myfields); print "</pre>"; The field objects now have 'len', 'values' and 'flags' parameters. NOTE: 'values' only has data for set and enum fields. //This assumes an open database connection //I also use a constant DB_DB for current database. function GetFieldInfo($table) { if($table == '') return false; $fields = mysql_list_fields(DB_DB, $table); if($fields){ $columns = mysql_query('show columns from ' . $table); if($columns){ $num = mysql_num_fields($fields); for($i=0; $i < $num; ++$i){ $column = mysql_fetch_array($columns); $field = mysql_fetch_field($fields, $i); $flags = mysql_field_flags($fields, $i); if($flags == '') $flags=array(); else $flags = explode(' ',$flags); if (ereg('enum.(.*).',$column['Type'],$match)) $field->values = explode(',',$match[1]); if (ereg('set.(.*).',$column['Type'],$match)) $field->values = explode(',',$match[1]); if(!$field->values) $field->values = array(); $field->flags = $flags; $field->len = mysql_field_len($fields, $i); $result_fields[$field->name] = $field; $result_fields[$i] = $field; } mysql_free_result($columns); } mysql_free_result($fields); return $result_fields; } return false; } hope someone else finds this useful. -- http://www.php.net/manual/en/function.mysql-fetch-field.php http://master.php.net/manage/user-notes.php?action=edit+20082 http://master.php.net/manage/user-notes.php?action=delete+20082 http://master.php.net/manage/user-notes.php?action=reject+20082

« previous php.notes (#28046) next »