Michael Joseph Aubry
Michael Joseph Aubry

Reputation: 13452

How to write simple database query?

I am starting to get the hang of getting data from the database, not as hard as it looks, but writing DB queries and echoing them can get a little confusing...

Here is how the table is setup:

table

I am trying to query the DB and get the value of meta_value WHERE meta_key = '_moon_sortable_content'

    // Get WPDB Object
    global $wpdb;

    // Table name
    $table_name = $wpdb->prefix . "postmeta";

    // My Query
    $bulls = $wpdb->get_results( "SELECT * FROM $table_name
                                        WHERE meta_key = '_moon_sortable_content'" );

Here I am trying to get the values...

    foreach($bulls as $key => $value ) {
      echo '<li>'.$item.'</li>';
    }

Here is the HTML output:

<li>0</li>
<li>1</li>
<li>2</li>

More Details: The value inside meta_value is pixels, there are three rows with the meta_key '_moon_sortable_content', so I am hoping to get the HTML output to be...

<li>297px</li>
<li>783px</li>
<li>this should actually be data from a textfield, so text for the result :)</li>

Update: I did var_dump on $value and it returns

object(stdClass)#282 (1) { ["meta_value"]=> string(5) "Array" }
object(stdClass)#283 (1) { ["meta_value"]=> string(5) "498px" }
object(stdClass)#284 (1) { ["meta_value"]=> string(20) "154.00001525878906px" } 

How do I clean that up into a variable?

Upvotes: 1

Views: 80

Answers (2)

user2605637
user2605637

Reputation: 11

SELECT meta_value FROM $table_name WHERE meta_key = '_moon_sortable_content'  

Upvotes: 1

Seth McClaine
Seth McClaine

Reputation: 10040

item is the key index string is the value

foreach ($bull as $key=>$value)

Upvotes: 1

Related Questions