Home >Database >Mysql Tutorial >How to Retrieve Enum Values from a MySQL Database for Dropdowns?

How to Retrieve Enum Values from a MySQL Database for Dropdowns?

Linda Hamilton
Linda HamiltonOriginal
2024-11-29 07:13:12611browse

How to Retrieve Enum Values from a MySQL Database for Dropdowns?

Retrieving Enum Possible Values from a MySQL Database

In cases where you seek to automatically populate dropdowns with enum possible values from a database, MySQL provides a viable solution. Here's a customizable function that caters to this specific need:

function get_enum_values( $table, $field )
{
    $type = fetchRowFromDB( "SHOW COLUMNS FROM {$table} WHERE Field = '{$field}'" )->Type;
    preg_match("/^enum\(\'(.*)\'\)$/", $type, $matches);
    $enum = explode("','", $matches[1]);
    return $enum;
}

This function extracts the enum values from the table's column definition and presents them in an array format. By integrating this function into your application, you can readily populate dropdowns with the corresponding enum options derived from the database.

The above is the detailed content of How to Retrieve Enum Values from a MySQL Database for Dropdowns?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn