aWebDeveloper
aWebDeveloper

Reputation: 38352

How do I get the default value of enum in PHP?

How do I get the default values of enum in PHP/MySQL? I wish to use it as a radio button/dropdown.

Upvotes: 2

Views: 2557

Answers (4)

Hafiz
Hafiz

Reputation: 4267

This will help you:

class enum_values {

public $values;

public function __construct($table, $column){

    $sql = "SHOW COLUMNS FROM $table LIKE '$column'";
    if ($result = mysql_query($sql)) { // If the query's successful

        $enum = mysql_fetch_object($result);
        preg_match_all("/'([\w ]*)'/", $enum->Type, $values);
        $this->values = $values[1];

    } else {

        die("Unable to fetch enum values: ".mysql_error());

    }

  }
}

I found this here: http://barrenfrozenwasteland.com/index.php?q=node/7

But one thing, I would like to mention that successful fetching columns also depends on the permissions that your db user have.

Upvotes: 1

Mchl
Mchl

Reputation: 62387

This query:

SELECT 
  COLUMN_TYPE 
FROM 
  information_schema.COLUMNS 
WHERE 
  TABLE_SCHEMA = 'yourDatabase' 
  AND TABLE_NAME = 'yourTable' 
  AND COLUMN_NAME = 'yourEnumColumn'

Will return a column type string like enum('v1','v2','v3'). You can then use simple string operations (and maybe explode) to convert it to whatever format suits you.

Upvotes: 2

ajreal
ajreal

Reputation: 47321

mysql> create table choices ( choice enum('a','b') not null default 'b');
Query OK, 0 rows affected (0.00 sec)

mysql> desc choices;
+--------+---------------+------+-----+---------+-------+
| Field  | Type          | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| choice | enum('a','b') | NO   |     | b       |       |
+--------+---------------+------+-----+---------+-------+

mysql> show columns from choices like 'choice';
+--------+---------------+------+-----+---------+-------+
| Field  | Type          | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| choice | enum('a','b') | NO   |     | b       |       |
+--------+---------------+------+-----+---------+-------+

either query able to result on Default field

Upvotes: 3

RabidFire
RabidFire

Reputation: 6330

This is a snippet from the CakePHP bakery which you can use for your purposes. It will give you the general idea:

//Get the values for the specified column (database and version specific, needs testing)
$result = $this->query("SHOW COLUMNS FROM {$tableName} LIKE '{$columnName}'");

//figure out where in the result our Types are (this varies between mysql versions)
$types = null;
if     ( isset( $result[0]['COLUMNS']['Type'] ) ) { $types = $result[0]['COLUMNS']['Type']; $default = $result[0]['COLUMNS']['Default']; } //MySQL 5
elseif ( isset( $result[0][0]['Type'] ) ) { $types = $result[0][0]['Type']; $default = $result[0][0]['Default']; } //MySQL 4
else { return array(); } //types return not accounted for

//Get the values
$values = explode("','", preg_replace("/(enum)\('(.+?)'\)/","\\2", $types) );

Hope that helps!

Upvotes: 1

Related Questions