1

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?

5
  • 4
    I guess you are asking question every few hours, perhaps you should learn the basics... Commented Jul 22, 2013 at 5:42
  • Questions are a good thing... I am taking very careful thought into eachone and spending some good time writing them up, it only takes a few min to answer.. Commented Jul 22, 2013 at 5:43
  • When you're echoing your data you've echoed the index to the array instead of the value. Commented Jul 22, 2013 at 5:43
  • @MichaelJosephAubry Not the down voter here.. and it only takes few mins to answer, but this is not how this site works, you will be question banned if you asked frequently and your questions get down voted, just an info to you.. Commented Jul 22, 2013 at 5:44
  • Any good tuts on writing DB queries? Commented Jul 22, 2013 at 5:56

2 Answers 2

1

item is the key index string is the value

foreach ($bull as $key=>$value)
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, that made me rethink, I couldnt echo the $value but I ran var_dump I updated the OP, how do I turn it into a varibale?
1
SELECT meta_value FROM $table_name WHERE meta_key = '_moon_sortable_content'  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.