Jump to content

get enum variables


lipun4u

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/176573-get-enum-variables/
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/176573-get-enum-variables/#findComment-930871
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.