2

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

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Marina Santini
  • 99
  • 1
  • 3
  • 12
  • $wpbd->get_result() seems to return an object, aren't there an analogue function that returns an array instead? Check in WP Codex – Damien Pirsy Oct 11 '11 at 10:05

2 Answers2

2

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
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
0
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;
}
vzwick
  • 11,008
  • 5
  • 43
  • 63
  • 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 Santini Oct 11 '11 at 11:05
  • @MarinaSantini $webPageArticle, is not a string, as mentioned in the answer, it is an array.. instead of echo, do a print_r($webPageArticle), and you will see the array content. – Rakesh Mehta Sep 16 '22 at 17:43