wpdb::get_col_info()publicWP 0.71

Gets an array with information about the columns of the last query.

If the columns are not defined in the query, the function will return information about all columns of the table. This can be useful when an object has been returned, about which we know nothing.

It works based on cache, so you first need to make a request using get_results(), get_col(), get_var(), etc., and then call this function - it will return the column data of the last query.

Method of the class: wpdb{}

No Hooks.

Returns

Mixed. Column data.

Usage

global $wpdb;
$wpdb->get_col_info( $info_type, $col_offset );
$info_type(string)

Indicates what information we need to obtain. List of possible values:

  • name - column name.
  • table - name of the table to which the column belongs.
  • max_length - maximum length of the column data.
  • not_null - 1 if the column cell cannot accept NULL value.
  • primary_key - 1 if the column is a primary key.
  • unique_key - 1 if the column cells must always be unique.
  • multiple_key - 1 if the column cells can be non-unique.
  • numeric - 1 if the column contains numeric data.
  • blob - 1 if the column contains BLOB type data (binary data).
  • type - column type.
  • unsigned - 1 if the column has UNSIGNED data type.
  • zerofill - 1 if the column has ZEROFILL data type.

Default: name

$col_offset(number)

A pointer to which column's information needs to be obtained:

  • -1 — information about all columns will be obtained as an array. Default.
  • 0, 1, 2, ... — information about the specified column will be returned, where 0 - first column, 1 - second, etc.

Default: -1

Examples

0

#1 Demo of work

Let's make a query:

global $wpdb;

$results = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta" );

Now let's get the data about the table columns of this query:

$cols_data = $wpdb->get_col_info( 'name' );
/*
Array
(
	[0] => meta_id
	[1] => post_id
	[2] => meta_key
	[3] => meta_value
)
*/

$cols_data = $wpdb->get_col_info( 'max_length' );
/*
Array
(
	[0] => 6
	[1] => 5
	[2] => 45
	[3] => 20205
)
*/

$cols_data = $wpdb->get_col_info( 'type' );
/*
Array
(
	[0] => 8
	[1] => 8
	[2] => 253
	[3] => 252
)
*/

What happens if you specify a column name that does not exist:

$cols_data = $wpdb->get_col_info( 'primary_key' );
/*
Notice: Undefined property: stdClass::$primary_key in /wpexample.com/public_html/wp-includes/wp-db.php on line 3435
Notice: Undefined property: stdClass::$primary_key in /wpexample.com/public_html/wp-includes/wp-db.php on line 3435
Notice: Undefined property: stdClass::$primary_key in /wpexample.com/public_html/wp-includes/wp-db.php on line 3435
Notice: Undefined property: stdClass::$primary_key in /wpexample.com/public_html/wp-includes/wp-db.php on line 3435

Array
(
	[0] =>
	[1] =>
	[2] =>
	[3] =>
)
*/

To get the data of an individual column, you need to specify its index in the second parameter:

echo $wpdb->get_col_info( 'name', 0 );       //> meta_id
echo $wpdb->get_col_info( 'name', 1 );       //> post_id
echo $wpdb->get_col_info( 'max_length', 0 ); //> 6
echo $wpdb->get_col_info( 'max_length', 1 ); //> 5

Changelog

Since 0.71 Introduced.

wpdb::get_col_info() code WP 6.9.1

public function get_col_info( $info_type = 'name', $col_offset = -1 ) {
	$this->load_col_info();

	if ( $this->col_info ) {
		if ( -1 === $col_offset ) {
			$i         = 0;
			$new_array = array();
			foreach ( (array) $this->col_info as $col ) {
				$new_array[ $i ] = $col->{$info_type};
				++$i;
			}
			return $new_array;
		} else {
			return $this->col_info[ $col_offset ]->{$info_type};
		}
	}
}