I have queried the database in WordPress using get_results()
and I get the following when I dump the $myrows
variable. How do I echo
each part?
array(1) {
[0]=>object(stdClass)#215 (2) {
["location_id"]=> string(1) "5"
["location_name"]=> string(9) "Liverpool"
}
}
I would like to be able to have the following the variable dumped is $myrows
:
echo '<p>' . $myrows['location-name'] . '</p>';
What you’re seeing there is an array of objects (within only a single item in that array).
Alternatively, you can update
get_results()
to return the items as an array of arrays.For example:
Typically, you would use
get_results()
to get multiple rows, so you would useforeach()
or something similar:If you only wanted to get a single row, you can use the
get_row($query, ARRAY_A)
method instead.There’s lot of information about the
$wpdb
class in the WordPress Codex.if echo total of an array