wpdb::get_row()publicWP 0.71

Retrieves one row from the database.

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

Method of the class: wpdb{}

No Hooks.

Return

Array|Object|null|null. Database query result in format specified by $output or null on failure.

Usage

global $wpdb;
$wpdb->get_row( $query, $output, $y );
$query(string|null)
SQL query.
Default: null
$output(string)
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: OBJECT
$y(int)
Row to return. Indexed from 0.

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