最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Converting mysql to $wpdb

programmeradmin1浏览0评论

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);
    if(!empty($enum_array[1]))
    {
      //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;
    }
    else
      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

<?php
            $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>';
            }
?>

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);
    if(!empty($enum_array[1]))
    {
      //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;
    }
    else
      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

<?php
            $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>';
            }
?>
Share Improve this question edited Dec 8, 2015 at 23:48 Egert Aia asked Dec 8, 2015 at 23:38 Egert AiaEgert Aia 1213 bronze badges 1
  • 1 of note, the mysql extension is deprecated, the mysql_query function and others will throw warnings in recent versions of PHP, and will fail completely in the newest versions ( the extension was removed in favour of mysqli and PDO ) – Tom J Nowell Commented Dec 9, 2015 at 3:13
Add a comment  | 

2 Answers 2

Reset to default 0

You can query INFORMATION_SCHEMA and achieve the same:

function getEnumValues($table, $field)
  {

    global $wpdb;

    $result = $wpdb->get_row("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" . DB_NAME . "' AND TABLE_NAME = '" . $table . "' AND COLUMN_NAME LIKE '" . $field . "'");

    if($result === FALSE) {
      die($wpdb->last_error); 
    }

    // this is the column name
    $result->COLUMN_NAME;

}

Here is a solution for a WordPress table field. This will break if the ENUM values have comma in them

https://gist.github/ahmu83/358f8e41e6134013991d9e3cf5edaf8c

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论