https://github.com/emikodes/php-select-options-mysql-enum
Generates a "Select" type element, with options taken from an ENUM data type in MySQL.
https://github.com/emikodes/php-select-options-mysql-enum
enum mysql mysql-html mysql-select php php-dynamic-html-select-mysql php-enum-dropdown php-mysql-html-select select select-enum
Last synced: 17 days ago
JSON representation
Generates a "Select" type element, with options taken from an ENUM data type in MySQL.
- Host: GitHub
- URL: https://github.com/emikodes/php-select-options-mysql-enum
- Owner: emikodes
- Created: 2023-02-13T13:31:31.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-06T11:17:14.000Z (about 3 years ago)
- Last Synced: 2025-09-01T10:15:43.951Z (9 months ago)
- Topics: enum, mysql, mysql-html, mysql-select, php, php-dynamic-html-select-mysql, php-enum-dropdown, php-mysql-html-select, select, select-enum
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PHP “SELECT” Dropdown menu with MySQL ENUM:
---
> This code snippet written in **PHP**, generates a “**Select**” input type in **HTML**, by taking the allowed values from a **MySQL ENUM** data type.
>
---
## Code:
```php
HTML;
if($myDBObject = new mysqli("**SERVER_HOSTNAME**","**MySQL_USERNAME**","**MySQL_PASSWORD**")){
$myDBObject->select_db("DB_NAME");
if(!$queryResult = $myDBObject->query("SHOW COLUMNS FROM **tableName** WHERE Field=\"**columnName**\"")){
echo "
There was an error while creating the page.
";
}else{
if($queryResult->num_rows == 0){
echo "Check the table definition.";
}else{
echo"
**Title**:
";
$enumString = $queryResult->fetch_array()['Type'];
$enumString = substr($enumString,6,strlen($enumString)-8);
$enumString = str_replace('\'','',$enumString);
$arrayEnumOptions = explode(',',$enumString);
echo "";
foreach($arrayEnumOptions as $singleOption){
echo "$singleOption";
}
echo "";
echo $HTMLPage;
}
}
}else{
echo "
There was an error while creating the page.
";
}
?>
```
---
## Technologies Used
- **PHP**
- **MySQLI**
---
## Usage
Just copy-paste my code snippet in your **.php** file, and modify the highlighted parts according to your needs.
You can insert **HTML** code in the “**$HTMLPage**” variable, to only show that code if the dropdown menu has been correctly generated.
For example, if you don’t want your form to be submittable if the dropdown menu wasn’t generated, insert the submit button code in that part.
---
## Contact
Coded by @emikodes - feel free to contact me!
---