Converting mysql to $wpdb


I’m trying to convert this to using a $wpdb class.
It will return all the enums possible and i have to use this $wpdb due to mysql_query giving me weird error (no database selected)
Code is following

function getEnumValues($table, $field)
    $enum_array = array();
    $query = 'SHOW COLUMNS FROM `' . $table . '` LIKE "' . $field . '"';
    $result = mysql_query($query);

    if($result === FALSE) {
    die(mysql_error()); }

    $row = mysql_fetch_row($result);
    preg_match_all('/'(.*?)'/', $row[1], $enum_array);
      //Shift array keys to match original enumerated index in MySQL (allows for use of index values instead of strings)
      foreach($enum_array[1] as $mkey => $mval) $enum_fields[$mkey+1] = $mval;
      return $enum_fields;
      return array(); // Return an empty array to avoid possible errors/warnings if array is passed to foreach() without first being checked with !empty().

Afterwards I have to use this code snippet to read them out

            $enums = getEnumValues("property", "form_field_type");
            foreach($enums as $enum){
                  echo '<input type = "radio" name = "form_field_type" value = "'.$enum.'">';
                  echo '<label for = "'.$enum.'"> '.$enum.'</label><br>';
Egert Aia 4 months 0 Answers 13 views 0

Leave an answer