wpdb::get_row() WordPress Method

The wpdb::get_row() method is a useful tool for retrieving data from a database table. This method takes two parameters: the first is the SQL query to be executed, and the second is an array of values to be passed to the query. The wpdb::get_row() method returns an object containing the data from the first row of the result set, or false if no data was returned.

wpdb::get_row( string|null $query = null, string $output = OBJECT, int $y ) #

Retrieves one row from the database.


Description

Executes a SQL query and returns the row from the SQL result.


Top ↑

Parameters

$query

(string|null)(Optional)SQL query.

Default value: null

$output

(string)(Optional) The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to an stdClass object, an associative array, or a numeric array, respectively.

Default value: OBJECT

$y

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


Top ↑

Return

(array|object|null|void) Database query result in format specified by $output or null on failure.


Top ↑

Source

File: wp-includes/wp-db.php

	public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
		$this->func_call = "\$db->get_row(\"$query\",$output,$y)";

		if ( $query ) {
			if ( $this->check_current_query && $this->check_safe_collation( $query ) ) {
				$this->check_current_query = false;
			}

			$this->query( $query );
		} else {
			return null;
		}

		if ( ! isset( $this->last_result[ $y ] ) ) {
			return null;
		}

		if ( OBJECT === $output ) {
			return $this->last_result[ $y ] ? $this->last_result[ $y ] : null;
		} elseif ( ARRAY_A === $output ) {
			return $this->last_result[ $y ] ? get_object_vars( $this->last_result[ $y ] ) : null;
		} elseif ( ARRAY_N === $output ) {
			return $this->last_result[ $y ] ? array_values( get_object_vars( $this->last_result[ $y ] ) ) : null;
		} elseif ( OBJECT === strtoupper( $output ) ) {
			// Back compat for OBJECT being previously case-insensitive.
			return $this->last_result[ $y ] ? $this->last_result[ $y ] : null;
		} else {
			$this->print_error( ' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N' );
		}
	}


Top ↑

Changelog

Changelog
VersionDescription
0.71Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.