2012-08-28 3 views
9

가능한 중복 : 나는 Mysql에 coloumn를 설정 한
Mysql Select Enum ValuesPHP를 사용하여 MySQL 데이터베이스에서 가능한 값을 열거 할 수 있습니까?

:

유형 : ENUM

Length/Values: '01','02','03','04','05','06','07','08','09','10','11','12'

나는 DB에서 해당 값을 가져 오지하기 위해 노력하고있어 :

나는 같은 질문 다른 게시물을 찾았지만 내 코드는 내가 얻을

$type = $mysqli->query("SHOW COLUMNS FROM {$tableName} WHERE Field = 'type'")->fetch_object()->Type; 
     preg_match('/^enum\((.*)\)$/', $type, $matches); 
     foreach(explode(',', $matches[1]) as $value) 
     { 
      $enum[] = trim($value, "'"); 
     } 
     return $enum; 

를 작동하지 않았다 유형 Text Insted ENUM

+1

[http://stackoverflow.com/questions/4644220/mysql-select-enum-values] 이것은 새로운 질문이 아닙니다.보세요. [1] : http://stackoverflow.com/questions/4644220/mysql-select-enum-values ​​ – tijs

답변

8

information_schema의 정보를 구문 분석해야합니다. columns 테이블 -

SELECT 
    column_type 
FROM 
    information_schema.columns 
WHERE 
    table_schema = 'your_schema' AND table_name = 'your_table' AND column_name = 'your_column' 

... 또 다른 쿼리 - enum('01','02','03') -

SELECT 
    TRIM(TRAILING ')' FROM TRIM(LEADING '(' FROM TRIM(LEADING 'enum' FROM column_type))) column_type 
FROM 
    information_schema.columns 
WHERE 
    table_schema = 'your_schema' AND table_name = 'your_table' AND column_name = 'your_column'; 

이런 일이있을 것입니다. 이 문자열을 PHP 응용 프로그램에서 구문 분석하십시오.

+0

감사합니다. 매력을 느낀다 – devmonster

+0

MySQL의 모든 열거 형에 암시 적으로 존재하는'' ''값을 추가하는 것을 잊지 마십시오. 필드가 널 (NULL) 입력 가능한 경우에는 NULL입니다. – aib

관련 문제