note 76836 added to function.mysqli-fetch-field
| From: | SamB at osu1 dot php dot net | Date: | Wed, 01 Aug 2007 17:03:34 +0000 |
| Subject: | note 76836 added to function.mysqli-fetch-field | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129107@lists.php.net to get a copy of this message | ||
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.
?>
----
Server IP: 208.69.120.35
Probable Submitter: 134.121.227.168
----
Manual Page -- http://www.php.net/manual/en/function.mysqli-fetch-field.php
Edit -- https://master.php.net/note/edit/76836
Del: integrated -- https://master.php.net/note/delete/76836/integrated
Del: useless -- https://master.php.net/note/delete/76836/useless
Del: bad code -- https://master.php.net/note/delete/76836/bad+code
Del: spam -- https://master.php.net/note/delete/76836/spam
Del: non-english -- https://master.php.net/note/delete/76836/non-english
Del: in docs -- https://master.php.net/note/delete/76836/in+docs
Del: other reasons-- https://master.php.net/note/delete/76836
Reject -- https://master.php.net/note/reject/76836
Search -- https://master.php.net/manage/user-notes.php