note 20082 modified in function.mysql-fetch-field by danbrown

From: Date: Sat, 20 Jun 2009 02:26:10 +0000
Subject: note 20082 modified in function.mysql-fetch-field by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-157166@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: <?php $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. <?php //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. --was-- 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: <?php $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. <?php //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://php.net/manual/en/function.mysql-fetch-field.php

« previous php.notes (#157166) next »