note 76836 deleted from function.mysqli-fetch-field by nlopess
| From: | nlopess@php.net | Date: | Wed, 08 Aug 2007 17:17:31 +0000 |
| Subject: | note 76836 deleted from function.mysqli-fetch-field by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-129342@lists.php.net to get a copy of this message | ||
Note Submitter: Sam B
----
Workaround for SET/ENUM type confusion with STRING...
See the bug report as posted on [svn.mysql.com]
http://preview.tinyurl.com/37seku
(If the link above isn't available, google "mysqli type set enum string", and find
the entry starting with "FishEye:" ... It's pretty long so I opted to go with a
tinyurl)
Using fetch_field (and similar) from the MySQLi extension, SET and ENUM fields are incorrectly given
type MYSLQI_TYPE_STRING.
To correct this, I query the database "SHOW FIELDS FROM table LIKE 'fieldname'"
and parse the results (specifically for SET and ENUM).
Example:
<?php
// $conn: MySQLI Connection
// $res: MySQLI Query Result
// $fieldname refers to a field of type SET or ENUM
$fieldName = "fieldname";
$fieldStruct = $res->fetch_fields();
$field = & $fieldStruct[$fieldName];
// Check returned type
echo $field->type;
// Fields of SET and ENUM will have type 254, MYSQLI_TYPE_STRING
// Ask DB for "human readable" structural info
$res = $conn->query("SHOW FIELDS FROM table LIKE '$fieldname'");
$row = $res->fetch_array(MYSQLI_ASSOC);
// Workaround begins here...
if($field->type == MYSQLI_TYPE_STRING){
if(preg_match('/^enum/', $row['Type']) > 0){
$field->type = MYSQLI_TYPE_ENUM;
elseif(preg_match('/^set/', $row['Type']) > 0){
$field->type = MYSQLI_TYPE_SET;
}
// ... derive options from $row['Type'] if desired.
}
// End workaround.
?>