最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - foreach not returning expected results

programmeradmin0浏览0评论

I am having issues returning a valid response from the below code. The intention is to return a list of cities based upon post_title().

The print_r response indicates a response is being processed (see below) however when an echo on $city is undertaken it sends an error.

add_shortcode('citylist',function(){
global $wpdb;
$country = $wpdb->prefix . 'worldcities';
$title = wp_title("",false,'right');
$args = $wpdb->get_results( "SELECT city_ascii FROM $country WHERE ranking < 2 AND country = '$title'");
foreach ($args as $city_ascii=>$city) {
        print_r ($city);;
};
} );

The print _r response is as follows (note Page_Title is "Nigeria".

StdClass Object ([city_ascii] => Ibadan) StdClass Object ([city_ascii] => Ogbomoso ...

All of the responses are expected and correct (ie. Ibadan, Ogbomoso etc).

However when this is replaced with echo, it throws an error. (see revised code below).

add_shortcode('citylist',function(){
global $wpdb;
$country = $wpdb->prefix . 'worldcities';
$title = wp_title("",false,'right');
$args = $wpdb->get_results( "SELECT city_ascii FROM $country WHERE ranking < 2 AND country = '$title'");
foreach ($args as $city_ascii=>$city) {
        echo $city;;
};
} );

Any help would be appreciated.

(Note I have read the PHP manuals, reviewed previous posts here and on other boards already before anyone suggests this avenue).

I am having issues returning a valid response from the below code. The intention is to return a list of cities based upon post_title().

The print_r response indicates a response is being processed (see below) however when an echo on $city is undertaken it sends an error.

add_shortcode('citylist',function(){
global $wpdb;
$country = $wpdb->prefix . 'worldcities';
$title = wp_title("",false,'right');
$args = $wpdb->get_results( "SELECT city_ascii FROM $country WHERE ranking < 2 AND country = '$title'");
foreach ($args as $city_ascii=>$city) {
        print_r ($city);;
};
} );

The print _r response is as follows (note Page_Title is "Nigeria".

StdClass Object ([city_ascii] => Ibadan) StdClass Object ([city_ascii] => Ogbomoso ...

All of the responses are expected and correct (ie. Ibadan, Ogbomoso etc).

However when this is replaced with echo, it throws an error. (see revised code below).

add_shortcode('citylist',function(){
global $wpdb;
$country = $wpdb->prefix . 'worldcities';
$title = wp_title("",false,'right');
$args = $wpdb->get_results( "SELECT city_ascii FROM $country WHERE ranking < 2 AND country = '$title'");
foreach ($args as $city_ascii=>$city) {
        echo $city;;
};
} );

Any help would be appreciated.

(Note I have read the PHP manuals, reviewed previous posts here and on other boards already before anyone suggests this avenue).

Share Improve this question asked Jan 9, 2021 at 22:11 BretMaddenBretMadden 31 bronze badge 3
  • Before going deeper: what do you see if you use echo $city[0]; or echo $city_ascii[0];? Also, what do you see when you change the foreach to foreach ($args as $city_ascii) { ? – Celso Bessa Commented Jan 9, 2021 at 22:20
  • On both instances I get an "Uncaught Error Cannot Use object of type stdClass as array .." which is why I am slightly confused. – BretMadden Commented Jan 9, 2021 at 22:28
  • 1 This code contains a major SQL injection attack, you need to use $wpdb->prepare to sanitise and validate your variables instead of just including them directly – Tom J Nowell Commented Jan 9, 2021 at 23:27
Add a comment  | 

1 Answer 1

Reset to default 0

Your print_r() statement shows that your $wpdb call is returning an array of objects, and you can't echo an object. (Unless the object has a __toString() method, but stdClass doesn't.)

You need to get the property you seek from the object, like this: $city->city_ascii.

Also, you shouldn't echo the content generated by a shortcode. You should instead return the string.

Code edited to use $wpdb->prepare() to protect against SQL injection (per @TomJNowell's comment).

add_shortcode('citylist',function(){
    global $wpdb;
    $content = '';
    $country = $wpdb->prefix . 'worldcities';
    $title = wp_title("",false,'right');
    $args = $wpdb->get_results( 
                $wpdb->prepare(
                   "SELECT city_ascii FROM %s WHERE ranking < 2 AND country = %s",
                   $country,
                   $title
            )
    );
    foreach ($args as $city_ascii=>$city) {
            $content .= esc_html( $city->city_ascii ) . '<br />';
    };
    return $content;
} );
发布评论

评论列表(0)

  1. 暂无评论