php 在php中显示mysql枚举值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3715864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
displaying mysql enum values in php
提问by ktm
Hi friends what is the best way to show the mysql enum values while inserting and updating php page ?
嗨,朋友们,在插入和更新 php 页面时显示 mysql 枚举值的最佳方法是什么?
name ENUM('small', 'medium', 'large')
edited: actually I was asking for this . I have a database field
编辑:实际上我是在要求这个。我有一个数据库字段
`color` enum('red','blue','green','white') DEFAULT NULL,
and php form
和 php 表单
<label for="color">Colors</label><br />
<input type="text" name="color" />
How do i display enum value on the php form from mysql database? Please help
如何在 mysql 数据库中的 php 表单上显示枚举值?请帮忙
回答by shamittomar
If you have a enum field, the MySQL will return string values for it and you can set with integer and string values too.Simply doing this:
如果您有一个枚举字段,MySQL 将为其返回字符串值,您也可以使用整数和字符串值进行设置。只需这样做:
mysql_query("select name from tablename");
will give you full labels like small
or medium
or large
会给你充分的标签,像small
或medium
或large
And you can similarly update them too using full labels like this:
您也可以使用像这样的完整标签来类似地更新它们:
mysql_query("insert into tablename (name) values ('small')");
or numeric values like this:
或像这样的数值:
mysql_query("update tablename set name = 2");
回答by halfdan
You'd need to do a "SHOW COLUMNS FROM " to get the table schema. You could then go ahead and parse every line.
您需要执行“SHOW COLUMNS FROM”来获取表架构。然后你可以继续解析每一行。
$field = "enumField"; // The field that contains the ENUM
$result=mysql_query('show columns from '.$table.';');
while($tuple=mysql_fetch_assoc($result))
{
if($tuple['Field'] == $field)
{
$types=$tuple['Type'];
$beginStr=strpos($types,"(")+1;
$endStr=strpos($types,")");
$types=substr($types,$beginStr,$endStr-$beginStr);
$types=str_replace("'","",$types);
$types=split(',',$types);
if($sorted)
sort($types);
break;
}
}
You now have an array containing the possible values of your ENUM in $types
.
Note:That code is a quick-hack. Could be a little more tidy :)
您现在有一个数组,其中包含 ENUM 中的可能值$types
。
注意:该代码是一个快速黑客。可以更整洁一点:)
回答by Andrew
You can get an array of all possible enum values using the following function:
您可以使用以下函数获取所有可能的枚举值的数组:
function enum_values($table_name, $column_name) {
$sql = "
SELECT COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '" . mysql_real_escape_string($table_name) . "'
AND COLUMN_NAME = '" . mysql_real_escape_string($column_name) . "'
";
$result = mysql_query($sql) or die (mysql_error());
$row = mysql_fetch_array($result);
$enum_list = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));
return $enum_list;
}
回答by Mischa
Your question is not very clear. If you mean what input method should I use in the form on my php page, the answer below is relevant.
你的问题不是很清楚。如果您的意思是我应该在我的 php 页面上的表单中使用哪种输入法,那么下面的答案是相关的。
You can use a dropdown:
您可以使用下拉菜单:
<select name="name">
<option value="small">small</option>
<option value="medium">medium</option>
<option value="large">large</option>
</select>
Radio buttons are another possibility:
单选按钮是另一种可能性:
<input type="radio" name="name" value="small"> small
<input type="radio" name="name" value="mediuim"> medium
<input type="radio" name="name" value="large"> large
回答by Faisal
In Codeigniteryou can display enum possible values:
在Codeigniter 中,您可以显示枚举可能的值:
function gender_enums($table , $field){
$query = "SHOW COLUMNS FROM ".$table." LIKE '$field'";
$row = $this->db->query("SHOW COLUMNS FROM ".$table." LIKE '$field'")->row()->Type;
$regex = "/'(.*?)'/";
preg_match_all( $regex , $row, $enum_array );
$enum_fields = $enum_array[1];
foreach ($enum_fields as $key=>$value)
{
$enums[$value] = $value;
}
return $enums;
}
回答by David Crowe
Without regular expressions, after obtaining the MYSQL ENUM string (e.g. ENUM('apples','oranges','pears') using "SHOW COLUMNS FROM table" to put the type into $field["Type"] you could use the following code based on the fact that only the odd numbered array elements after exploding with a single quote are useful:
在没有正则表达式的情况下,在使用“SHOW COLUMNS FROM table”获取 MYSQL ENUM 字符串(例如 ENUM('apples','oranges','pears') 将类型放入 $field["Type"] 后,您可以使用以下内容基于以下事实的代码:只有用单引号爆炸后的奇数数组元素才有用:
$t = explode( "'", $field["Type"] );
for($i=1; $i < count($t); $i += 2)
echo "<br>Value $i: ".$t[$i];
It is possible that some really evil choices of ENUM value names could cause problems.
ENUM 值名称的某些真正邪恶的选择可能会导致问题。