wpdb::get_col( string|null $query = null, int $x )

Retrieves one column from the database.

Description

Executes a SQL query and returns the column from the SQL result. If the SQL result contains more than one column, the column specified is returned. If $query is null, the specified column from the previous SQL result is returned.

Parameters

$query

(string|null) (Optional) SQL query. Defaults to previous query.

Default value: null

$x

(int) (Optional) Column to return. Indexed from 0.

Return

(array) Database query result. Array indexed from 0 by SQL result row number.

Source

File: wp-includes/wp-db.php

public function get_col( $query = null, $x = 0 ) {
		if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
			$this->check_current_query = false;
		}

		if ( $query ) {
			$this->query( $query );
		}

		$new_array = array();
		// Extract the column values.
		if ( $this->last_result ) {
			for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
				$new_array[ $i ] = $this->get_var( null, $x, $i );
			}
		}
		return $new_array;
	}

Changelog

Version Description
0.71 Introduced.

© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wpdb/get_col