Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.