Hi there:

I have a function that successfully displays a select menu generated from an enum field in a MySQL DB.

However, the place I want to use it is within an if/else structure, and as hard as I try, I can't do:

if(mysql_field_type($fields,$i) == 'blob') {
//----display field data in <textfield> (This is OK!)
}
else if(mysql_field_type($fields,$i) == 'enum') {
//---use select menu generation function here (This is not OK!)
}

Does anyone have any ideas??

Cheers very much.
Russ

    • [deleted]

    What's "not ok" about it? any errors? unexpected behaviour? what?

      Sorry Vincent, What I meant by 'not OK' is that the code:

      else if(mysql_field_type($fields,$i) == 'enum') {
      //---use select menu generation function here
      }

      Obviousely doesn't work as ENUM() is totally different field type to say BLOB() for example. (multiple values)

      I can get the 'enum' string from a DB result (using the code below), but I'm unable to extract it from outside of the while loop: (I don't want to encase the whole if-else structure within the loop, because while my select menu is now displayed, all the fields are then multiplied! I only need a single occurance of each!)

      $result = mysql_query("DESC $tabid");
      while(list($name,$type) = mysql_fetch_row($result)) {
      $this_is = substr($type,0,4);
      if($this_is == 'enum') {
      echo $this_is; //echoes 'enum' if an enum field exists
      }//----end if
      }//----end while

      Cheers.
      Russ

        I did this once before but it took me a while to figure it out. I dug into the phpmyadmin code to see how they do it. Worked great.

        I eventually abandoned the idea, because I figured that using ENUM fields were limiting my flexibility. They're great for "yes/no" -- or other things that are not likely to ever change. For yes/no pull-downs, I just hard code the yes/no in my HTML form.

        But if there's any chance that the choices could change, I'd recommend using a db table that lists the choices -- and use that as your source data for pull-downs. All you have to do is add another row in the table to add choices . . . you could even set up some sort of admin form where a person could add choices. But with ENUM fields, a person with privileges to change the table would have to make the change.

        Good luck.

          DLB:

          Cheers for the reply:

          However I'm in too deep! I could indeed just abandon the idea and go along with your seperate table plan, but I already have my if/else structure set up as:

          if(mysql_field_type($fields,$i) == 'blob') {
          $field_obj = $field_names;
          //----if any datetime fields then place 'em in default select menus (defined using vars set up at top of page)
          else if(mysql_field_type($fields,$i) == 'datetime') {
          //---use pre built date select menus
          $field_obj = $dayselect . $monthselect . $yearselect;
          }

          I already have a function to generate a select menu from the ENUM() field I just need to detect for the field so that I can call the function!

          Cheers for your help.
          Russ

          PS How does phpMyAdmin do it?

            • [deleted]

            This would be my choice also, forget the entire enum, and use a seperate tabble that holds the possibilities. In fact, most 'real' relational databases don't have an ENUM type, they only support the 'extra table' method.

            But, coming back to your problem Russ,
            the only way to find out wether it is an enum type is is by doing a 'describe table'.
            If you do that on the entire table, you can just filter the 'Type' field and get all the types for all the fields.

            It is simply not such a good idea to depend on resultsets to find out what kind of data is stored in the database, also because the database can return integers as strings etc.

            BTW, have a look at the annotations in the manual, there is a complete function to create dropdowns from ENUM types, may be usefull:

            http://www.php.net/manual/en/function.mysql-field-type.php

              Okay folks! I get the picture, and I AM inclined to agree with you both.

              Well it's been a day long adventure!
              Thanks for the pointers folks.

              Vincent - I'll be back with more, you know it! ;-)

              Cheers
              Russ

                • [deleted]

                I'll be ready for you Russ, bring it on! :-)

                  Write a Reply...