tech and other rants

list enum set from a mysql table field

March 3rd, 2009 in programming
 function enum_select( $table , $field ){
        $query = " SHOW COLUMNS FROM `$table` LIKE '$field' ";
        $result = mysql_query( $query ) or die( 'error getting enum field ' . mysql_error() );
        $row = mysql_fetch_array( $result , MYSQL_NUM );
        $regex = "/'(.*?)'/";
        //$regex = "/'[^"\\\r\n]*(\\.[^"\\\r\n]*)*'/";
        preg_match_all( $regex , $row[1], $enum_array );
        $enum_fields = $enum_array[1];
        //print_r($enum_fields);
        return($enum_fields);
}

If you just want to see the values uncomment the print_r line and execute the function:

enum_select(table1,field1);

Now for example if you want to create a dropdown with this array you do:

echo "";

The result will look something like this:
result

Tags: , , ,

Leave a Reply