Marina Santini
Marina Santini

Reputation: 99

Error: Object of class stdClass could not be converted to string -- implode() -- php

can anybody help me understand why I get the followint error:

Object of class stdClass could not be converted to string in... (the error is pointing to the line with implode(), see below)

when I run the following function?

  function selectFullArticle () {

    global $wpdb;

  $id=get_the_ID();


      $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id" );

 $webPageArticle= implode(" ",$webPageArticle);
 return $webPageArticle;

}

My aim is to return a string and not an array.

Maybe the array returned from a SELECT must be treated in a different way?

Thanks in advance,

Marina


Thanks for your answers. I am trying to display a webpage that I downloaded from the web and saved it in a wordpress database, and not a post.

Both $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_N); and $webPageArticle = $wpdb->get_results( "SELECT post_content_long FROM $wpdb->posts WHERE ID=$id",ARRAY_A);

work well and implode() does not complain anymore. However, I do not get a real string, because the statement "echo $webPageArticle;" visualizes the word "Array" on the screen. T

how come?

marina

Upvotes: 2

Views: 3173

Answers (2)

Damien Pirsy
Damien Pirsy

Reputation: 25435

As read in Codex, you can pass an additional second parameter to get_result() to allow it to return an array instead of an object

 <?php $wpdb->get_results('query',OBJECT_K); ?> 

returns an associative array you can then manipulate.

Reference:

OBJECT - result will be output as a numerically indexed array of row objects.
OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.
ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays

Upvotes: 2

vzwick
vzwick

Reputation: 11044

function selectFullArticle () {
    $id=get_the_ID();
    $webPageArticle = get_post($id, ARRAY_A);
    $webPageArticle= implode(" ", $webPageArticle);
    return $webPageArticle;
}

If I figured it right and you want the post content only, use this:

function selectFullArticle () {
    $id=get_the_ID();
    $webPageArticle = get_post($id);
    return $webPageArticle->post_content;
}

Upvotes: 0

Related Questions