note 22101 added to function.mysql-fetch-field
| From: | chrisshaffer at bellsouth dot net | Date: | Thu, 06 Jun 2002 15:22:42 +0000 |
| Subject: | note 22101 added to function.mysql-fetch-field | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31535@lists.php.net to get a copy of this message | ||
Slight error in the above comment:
$fieldLen = split("','",substr(1,-1,$fieldLen));
should read:
$fieldLen = split("','",substr($fieldLen,1,-1));
oops! ;)
I did take the above code (which saved me at least two hours worth of work), and massaged it into a
function:
function mysql_enum_values($tableName,$fieldName)
{
$result = mysql_query("DESCRIBE $tableName");
//then loop:
while($row = mysql_fetch_array($result))
{
//# row is mysql type, in format "int(11) unsigned zerofill"
//# or "enum('cheese','salmon')" etc.
ereg('^([^ (]+)(\((.+)\))?([ ](.+))?$',$row['Type'],$fieldTypeSplit);
//# split type up into array
$ret_fieldName = $row['Field'];
$fieldType = $fieldTypeSplit[1];// eg 'int' for integer.
$fieldFlags = $fieldTypeSplit[5]; // eg 'binary' or 'unsigned zerofill'.
$fieldLen = $fieldTypeSplit[3]; // eg 11, or 'cheese','salmon' for enum.
if (($fieldType=='enum' || $fieldType=='set') &&
($ret_fieldName==$fieldName) )
{
$fieldOptions = split("','",substr($fieldLen,1,-1));
return $fieldOptions;
}
}
//if the funciton makes it this far, then it either
//did not find an enum/set field type, or it
//failed to find the the fieldname, so exit FALSE!
return FALSE;
}
The most useful thing that I can think to do with this is to populate a HTML Dropdown box with it:
echo "<SELECT NAME=\"Select\" SIZE='1'>";
foreach($fieldOptions as $tmp)
{
echo "<OPTION>$tmp";
}
Hope this helps :D
--
http://www.php.net/manual/en/function.mysql-fetch-field.php
http://master.php.net/manage/user-notes.php?action=edit+22101
http://master.php.net/manage/user-notes.php?action=delete+22101
http://master.php.net/manage/user-notes.php?action=reject+22101