wpdb::get_row()
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{}
Uses: wpdb::query()
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
#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"
#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. |