Retrieving Data From Single Cell In Wordpress MySQL Table

2 replies
I am still working on my first plugin and am at the point where I want to get a single value from a table created by another plugin.

Their table has six columns named blog_ID, level, expire, gateway, term and amount.

I am interested in using the first three ... blog_ID, level and expire. blog_ID and level are numeric values. expire is a unix (or linux) epoch date/timestamp.

I am struggling to display the level associated with a particular blog_ID. Later, I hope to evaluate whether the expire field is greater than the current epoch date/timestamp or not.

In the code below, all goes fine until I try to display the value. My result is showing as 'ARRAY.'
Code:
<?PHP
// Testing by Kirk
global $blog_id;
global $wpdb;
$thisblog = $blog_id;
echo ('Blog #' . $thisblog);
?>.' is at Level #'. <?php
$res = $wpdb->get_results("SELECT level FROM wp_pro_sites WHERE blog_ID = '$thisblog'");
echo $res;
?>
<br />
What do I need to do to display the value of the 'level' field for a blog with an id equal to the value held in $thisblog?

Thanks in advance!
#$wpdb #cell #data #mysql #mysql database #retrieving #single #single cell #table #wordpres #wordpress
  • Profile picture of the author cgimaster
    Thats because $res is an array of your query, it pulls the data into a one-dimensional object

    echo $res->level;

    Also you might want to use:
    Code:
    get_row
    Instead of
    Code:
    get_results
    Since you just want 1 entry

    For more information see:
    http://codex.wordpress.org/Class_Reference/wpdb
    {{ DiscussionBoard.errors[7324743].message }}
  • Profile picture of the author SteveJohnson
    Hey Kirk,
    If you want a value from a single column and you're sure you want your query to return only one row (as is the case with yours, it appears), you can use $wpdb->get_var(), which returns a single value so your echo will work as written.

    Also remember to check your queries for errors or empty values, don't assume that a query will always return a result.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[7325946].message }}

Trending Topics