• Resolved kirkward

    (@kirkward)


    I am writing my first plugin and wish to retrieve 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.’

    <?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!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You need to read the documentation for WPDB a bit closer. 🙂

    You can use $wpdb->get_row($query) to get a row, or you can loop through the array returned by $wpdb->get_results();.

    As Michael pointed out, all good points or you can change your code to this.

    <?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'");
    if( is_array( $res ) ) {
       foreach( $res as $data ) {
           $level = $data->level;
           // If the above line fails try
           $level = $data['level']
           // If that line also fails do this
           print_r($data);
           // The above line will print our the contents of $data
       }
    }
    ?>

    Basically the error you are getting is because $wp->get_results will always return an array and your attempting to echo that array which you cannot do, instead you could change the line.

    echo $res;

    to

    print_r($res)

    and that will show you what is inside the array and then you can change up the echo statement to display the data, you might even get lucky and have it work by changing the

    echo $res;

    to simply be as

    echo $res[0];

    Hope that helps. However your best bet if your only returning one thing from the table is to change this line.

    $res = $wpdb->get_results(

    to this

    $res = $wpdb->get_var(

    That should also do the trick.

    Thread Starter kirkward

    (@kirkward)

    Thanks Michael,
    Thanks Adam,

    I apologize for not posting before I went to bed last night, but old geezers get worn out with this coding stuff. (I don’t see how you guys do it night and day!)

    Anyhoo, after spending most all of the second day on it, I wound up with this

    <?PHP
    // Testing by Kirk
    global $blog_id;
    global $wpdb;
    $thisblog = $blog_id;
    echo ("Blog #".$thisblog);
    ?> is at Level #<?php
    $level = $wpdb->get_var("SELECT level FROM wp_pro_sites WHERE blog_ID = '.$thisblog.' ");
    // Echo the user's level
    echo $level;
    ?>

    The biggest change was using ->get_var instead of ->get_results

    And, of course, finally realizing that I needed to keep that semi-colon after the MySQL query. I was getting confused after reading that an MySQL query wasn’t supposed to have a semi-colon behind it, until I realized that putting it in the parentheses did make it part of a PHP statement.

    Another question, how do you guys remember all this stuff?

    @kirkward – After developing and programming for 12 years you naturally remember a ton of stuff but you will never remember everything, you always forget things you haven’t used in awhile, every programmer will have to refer back to documentations once and awhile.

    Basically there is too much to remember but as long as you have resources like the http://codex.wordpress.org/, http://codex.wordpress.org/Function_Reference/, and http://php.net/manual/en/ you can quickly refresh your memory and solve almost any problem.

    Things like forgetting semi-colons will always be common but are minor because once you make the same mistake a thousand times over you’ll always know what not to do, that is the best and most frustrating thing about programming, if you want to become an expert in it you really need a strong passion for it (Especially if your self-taught).

    Most people eventually give up due to the frustrations and errors they make along the way but they don’t realize we’ve all been there, it’s the errors you overcome that help you excel at this kinda work.

    sanket.realistic

    (@sanketrealisticyahooin)

    Hi
    i am getting error here is my code

    global $wpdb;
    $result = $wpdb->get_var(
    “SELECT visa_status from wp_v_detail where uniquecode = ‘$in_id'” );
    echo $result;

    i want to get only current status and display to page.i am not able to see anything after submitting button

    Thanx
    Sanket

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Retrieving Data From Single Cell In MySQL Table’ is closed to new replies.