lipun4u Posted October 5, 2009 Share Posted October 5, 2009 This function calculates all enum variables in a table <?php include("database.php"); function getEnumValues($table, $field) { $enum_array = array(); $query = 'SHOW COLUMNS FROM `' . $table . '` LIKE "' . $field . '"'; $result = mysql_query($query); $row = mysql_fetch_row($result); $data =$row[1]; preg_match_all('/\'(.*?)\'/', $data, $enum_array); print_r($enum_array); } getEnumVAlues('shirt', 'style'); ?> the structure of the shirt table is mysql> show columns from shirt; +-------+---------------------------------------------+------+-----+---------+-- --------------+ | Field | Type | Null | Key | Default | E xtra | +-------+---------------------------------------------+------+-----+---------+-- --------------+ | id | smallint(5) unsigned | NO | PRI | NULL | a uto_increment | | style | enum('t-shirt','polo','dress') | NO | | NULL | | | color | enum('red','blue','orange','white','black') | NO | | NULL | | | owner | smallint(5) unsigned | NO | | NULL | | +-------+---------------------------------------------+------+-----+---------+-- --------------+ 4 rows in set (0.03 sec) Why the op array has two sub array with enum elements on each array ?? <body> Array ( [0] => Array ( [0] => 't-shirt' [1] => 'polo' [2] => 'dress' ) [1] => Array ( [0] => t-shirt [1] => polo [2] => dress ) ) </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/176573-get-enum-variables/ Share on other sites More sharing options...
Adam Posted October 5, 2009 Share Posted October 5, 2009 The first array contains results that matched the entire reg exp, the second are those that were matched within the first pair of brackets. You could use "PREG_SET_ORDER" like below to only return the first matches and after: preg_match_all('/\'(.*?)\'/', $data, $enum_array, PREG_SET_ORDER); Quote Link to comment https://forums.phpfreaks.com/topic/176573-get-enum-variables/#findComment-930871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.