Hi.

On Wed 2002-09-04 at 09:41:02 -0500, [EMAIL PROTECTED] wrote:
> At 07:05 AM 9/4/2002, you wrote:
> 
> >Is there a way I can get what the allowed values of an 'enum'
> >coulmn are in mysql? I want to eventually list these values in
> >a list box or something similar?
> 
> mixo,
>         I can't see an easy way of getting the enum for a particular 
> column. The best I could come up with (and hopefully someone can improve on 
> this) is to execute a "MySQLDump -d mydatabase>schema.txt" and then use PHP 
> (or whatever language you're using) to find the table.column name, search 
> for "enum(", parse out the values and put them into a text file or make an 
> PHP include file out of it.  Since this only needs to be done when the 
> enum's have changed, and this process formats it properly for your 
> language, loading the enum list will be quite fast at run time.
> 
> I thought of using "Describe table" but that will wrap a long enum list 
> onto several lines in columnar format and is much harder to parse than a 
> MySQLDump.

This is a solution in PHP I once hacked, though I never found the time
to do a clean up:

// Returns an array which contains the allowed values for the enum
// field $COLUMN in a MySQL table $TABLE. $DB is espected to be a
// valid connection handle to the MySQL server in question (retrieved
// via mysql_connect() or alike).
// TODO: more error checking
function db_extract_enum( $db, $table, $column )
{
  $query = "DESCRIBE $table";
  $result = mysql_query( $query ) or db_die( __FILE__, __LINE__, $query );
  while( $row = mysql_fetch_assoc( $result ) )
      if( $row['Field'] == $column )
          break;

  // CHECK: this is too volatile, look for a more general solution
  // (like looking for the parenthesis or alike)
  $tmp = substr( $row['Type'], 6, strlen($row['Type'])-8 );
  return explode( "','", $tmp );
}


As I said, never did a clean up, so it isn't pretty. But it gives the
general idea how one can handle this.

Regards,

        Benjamin.


PS: You'll have to replace db_die() by something appropriate.

-- 
[EMAIL PROTECTED]

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to