note 31381 added to function.mysql-query

From: Date: Sat, 19 Apr 2003 17:30:01 +0000
Subject: note 31381 added to function.mysql-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-46818@lists.php.net to get a copy of this message
Regarding the idea for returning all possible values of an enum field, the mySQL manual says that "SHOW COLUMNS FROM table LIKE column" should be used to do this. The function below (presumes db connection) will return an array of the possible values of an enum. function GetEnumValues($Table,$Column) { $dbSQL = "SHOW COLUMNS FROM ".$Table." LIKE '".$Column."'"; $dbQuery = mysql_query($dbSQL); $dbRow = mysql_fetch_assoc($dbQuery); $EnumValues = $dbRow["Type"]; $EnumValues = substr($EnumValues, 6, strlen($EnumValues)-8); $EnumValues = str_replace("','",",",$EnumValues); return explode(",",$EnumValues); } Cavaets: 1) If the LIKE matches more than one column you get the enum from the first, so be careful with the $Column argument 2) You can't have ',' as part of one of the enums (I guess mySQL would escape this, but I haven't tried) 3) If the field isn't an enum you'll get garbage back! This is just a quick example to show how to do it, some tidying up needs to be done (ie checking if the field is actually an enum) before it is perfect. -- http://www.php.net/manual/en/function.mysql-query.php http://master.php.net/manage/user-notes.php?action=edit+31381 http://master.php.net/manage/user-notes.php?action=delete+31381 http://master.php.net/manage/user-notes.php?action=reject+31381

« previous php.notes (#46818) next »