PodsData::get_table_columns PHP Method

get_table_columns() public static method

Gets column information from a table
Since: 2.0
public static get_table_columns ( string $table ) : array
$table string Table Name
return array
    public static function get_table_columns($table)
    {
        global $wpdb;
        self::query("SHOW COLUMNS FROM `{$table}` ");
        $table_columns = $wpdb->last_result;
        $table_cols_and_types = array();
        foreach ($table_columns as $table_col) {
            // Get only the type, not the attributes
            if (false === strpos($table_col->Type, '(')) {
                $modified_type = $table_col->Type;
            } else {
                $modified_type = substr($table_col->Type, 0, strpos($table_col->Type, '('));
            }
            $table_cols_and_types[$table_col->Field] = $modified_type;
        }
        return $table_cols_and_types;
    }

Usage Example

 /**
  * @param $params
  *
  * @return array|bool|int|mixed|null|void
  */
 public function prepare_pod($params)
 {
     /**
      * @var $wpdb WPDB
      */
     global $wpdb;
     if (!isset($params->pod)) {
         return pods_error(__('Invalid Pod.', 'pods'));
     }
     $pod = pods_sanitize(pods_clean_name($params->pod));
     if (!in_array("{$wpdb->prefix}pod_tbl_{$pod}", $this->tables)) {
         return pods_error(__('Table not found, it cannot be migrated', 'pods'));
     }
     $count = pods_query("SELECT COUNT(*) AS `count` FROM `@wp_pod_tbl_{$pod}`", false);
     if (!empty($count)) {
         $count = (int) $count[0]->count;
     } else {
         $count = 0;
     }
     $pod_type = pods_query("SELECT `id` FROM `@wp_pod_types` WHERE `name` = '{$pod}'", false);
     if (!empty($pod_type)) {
         $pod_type = (int) $pod_type[0]->id;
     } else {
         return pods_error(__('Pod not found, it cannot be migrated', 'pods'));
     }
     $fields = array('id');
     $field_rows = pods_query("SELECT `id`, `name`, `coltype` FROM `@wp_pod_fields` WHERE `datatype` = {$pod_type} ORDER BY `weight`, `name`");
     if (!empty($field_rows)) {
         foreach ($field_rows as $field) {
             if (!in_array($field->coltype, array('pick', 'file'))) {
                 $fields[] = $field->name;
             }
         }
     }
     $columns = PodsData::get_table_columns("{$wpdb->prefix}pod_tbl_{$pod}");
     $errors = array();
     foreach ($columns as $column => $info) {
         if (!in_array($column, $fields)) {
             $errors[] = "<strong>{$column}</strong> " . __('is a field in the table, but was not found in this pod - the field data will not be migrated.', 'pods');
         }
     }
     foreach ($fields as $field) {
         if (!isset($columns[$field])) {
             $errors[] = "<strong>{$field}</strong> " . __('is a field in this pod, but was not found in the table - the field data will not be migrated.', 'pods');
         }
     }
     if (!empty($errors)) {
         return pods_error(implode('<br />', $errors));
     }
     return $count;
 }