2011-01-07 54 views

답변

1

이것은 용도로 사용할 수있는 CakePHP bakery의 스 니펫입니다. 일반적인 아이디어를 줄 것입니다 :

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

희망이 있습니다!

+0

좋은 gr8 정확히 내가 무엇을 찾고 있었는지 – aWebDeveloper

3
 
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  |  | 
+--------+---------------+------+-----+---------+-------+

Default 필드

2

이 쿼리에 발생할 수 중 쿼리

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

enum('v1','v2','v3') 같은 열 형식 문자열을 반환합니다. 그런 다음 간단한 문자열 연산을 사용하여 (그리고 아마도 폭발) 원하는 형식으로 변환 할 수 있습니다.

1

이 당신을 도울 수 있습니다됩니다

클래스 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()); 

    } 

    } 
} 

내가 여기를 발견 http://barrenfrozenwasteland.com/index.php?q=node/7

그러나 한 가지, 그 성공을 언급하고 싶습니다 열을 가져 오는 것은 또한 db 사용자가 가지고있는 권한에 달려있다.

관련 문제