Title: Retrieve values from WordPress database
Last modified: August 20, 2016

---

# Retrieve values from WordPress database

 *  Resolved [fejanos](https://wordpress.org/support/users/fejanos/)
 * (@fejanos)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/retrieve-values-from-wordpress-database/)
 * **I want to display values from sql query next to a single post.**
    **This is
   the function I use:**
 *     ```
       add_action('show_etiqueta','et_get_post_user_etiqueta');
       function et_get_post_user_etiqueta( $post_id ){
       global $wpdb;
           $result = $wpdb->get_results($wpdb->prepare("SELECT meta_value, count(*) AS countof FROM $wpdb->commentmeta WHERE meta_key='et_comment_etiqueta' GROUP BY meta_value ORDER BY countof DESC LIMIT 5", $post_id));
          print_r($result);
       return $result;
       }
       ```
   
 * **I call it here:**
    `<?php do_action( 'show_etiqueta' ); ?>` **But all I get
   as output is this:** Array ( [0] => stdClass Object ( [meta_value] => test [countof]
   => 3 ) [1] => stdClass Object ( [meta_value] => inprediscible [countof] => 1 )[
   2] => stdClass Object ( [meta_value] => interesante [countof] => 1 ) [3] => stdClass
   Object ( [meta_value] => aqui va tu etiqueta [countof] => 1 ) [4] => stdClass
   Object ( [meta_value] => hurtest [countof] => 1 ) ) I tested the query in phpmyadmin
   to see if I get the result I want and it really gets the right result. Can Anyone
   help me to have the right result displayed? tHank you

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Brad Markle](https://wordpress.org/support/users/imh_brad/)
 * (@imh_brad)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/retrieve-values-from-wordpress-database/#post-2410174)
 * Hi fejanos. The data that you’re seeing is the results from the database. What
   you need to do is format the data. What you’re doing is printing the raw data,
   and now you need to print the data.
 * It may look a bit better if you print it:
    INSTEAD OF USING: `print_r($result);`
   TRY USING: `echo "<pre>"; print_r($result); echo "</pre>";`
 * If you want to print it, try looping through the data using php’s [foreach](http://php.net/manual/en/control-structures.foreach.php)
   function.
 * For example, this *may* work (not actually tested):
 *     ```
       echo "<table>";
       foreach($result as $key => $value)
       {
       	echo "	<tr>
       			<td>$key</td>
       			<td>" . $value->meta_value . "</td>
       			<td>" . $value->countof . "</td>
       		</tr>
       	";
       }
       echo "</table>";
       ```
   
 *  Thread Starter [fejanos](https://wordpress.org/support/users/fejanos/)
 * (@fejanos)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/retrieve-values-from-wordpress-database/#post-2410177)
 * **Thank you very much.**
 *  [Brad Markle](https://wordpress.org/support/users/imh_brad/)
 * (@imh_brad)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/retrieve-values-from-wordpress-database/#post-2410180)
 * Hi fejanos, I hope I was able to help! I’m not sure how much experience you have
   with PHP, but feel free to let me know if you have any follow up questions, I’m
   more than happy to help further.
 *  Thread Starter [fejanos](https://wordpress.org/support/users/fejanos/)
 * (@fejanos)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/retrieve-values-from-wordpress-database/#post-2410335)
 * Well I’ve been learning PHP for 2 years, it’s mostly tutorial following and script
   modifying but sometimes I have a hard time creating scripts if you know what 
   I mean. Now I’m facing with another issue that I try to solve and I’m working
   on: These values that I wanted to display before are words that users submit 
   through a custom comment field. The function above makes a top list where the
   top 5 are the most submitted words appear. I want to make that post specific.
   For each individual single post must have a top 5 words list. Now it makes a 
   top list from all the submitted words at all the posts.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Retrieve values from WordPress database’ is closed to new replies.

## Tags

 * [call function](https://wordpress.org/support/topic-tag/call-function/)
 * [result](https://wordpress.org/support/topic-tag/result/)
 * [wpdb->get_results](https://wordpress.org/support/topic-tag/wpdb-get_results/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [fejanos](https://wordpress.org/support/users/fejanos/)
 * Last activity: [14 years, 6 months ago](https://wordpress.org/support/topic/retrieve-values-from-wordpress-database/#post-2410335)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
