wpdb::get_row()publicWP 0.71

Gets the first row from the result of an SQL query. Returns the row as an object.

Use the $row_offset parameter to get the second, third, ..., n-th row from the query.

Method of the class: wpdb{}

No Hooks.

Returns

Array|Object|null|null.

  • object - when $output_type = OBJECT (default).
  • array - when $output_type = ARRAY_A or ARRAY_N.
  • null - when data could not be retrieved (requested data is not in the database).

Usage

$wpdb->get_row( $query, $output_type, $row_offset );
$query(string)
The query to be executed.
$output_type(constant)

One of three constants. Can be:

  • OBJECT - the result will be returned as an object (default).
  • ARRAY_A - the result will be returned as an associative array.
  • ARRAY_N - the result will be returned as a numerically indexed array.
    Default is OBJECT
$row_offset(number)
The number of the row returned from the query result.
Default is 0 (first row)

Examples

0

#1 Get all the information about link 10

$mylink = $wpdb->get_row( "SELECT * FROM $wpdb->links WHERE link_id = 10" );

// Now, the properties (variables) of $mylink are names
// columns from the table $wpdb->links with the values of the table fields:
echo $mylink->link_id; // display "10"
0

#2 Using a constant:

$mylink = $wpdb->get_row( "SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_A );

// the result will be an associative array
echo $mylink['link_id']; // display "10"

or

$mylink = $wpdb->get_row( "SELECT * FROM $wpdb->links WHERE link_id = 10", ARRAY_N );

// the result will be a numbered array
echo $mylink[1]; // display "10"

Changelog

Since 0.71 Introduced.

wpdb::get_row() code WP 6.9.1

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' );
	}
}