2011-01-07 10 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ユーザが持つ権限にも依存します。

関連する問題