如何将结果赋给变量?

时间:2017-01-08 作者:James

以下是我目前掌握的情况:

function get_highest_bid_info(){
    global $wpdb;
    $postid = get_the_id();
    $table = $wpdb->prefix . "jwp_bids";
    $wpdb->get_results(
        "SELECT email, max(bid_amt)
        FROM  $table
        WHERE post_id = \'$postid\')"
        );
现在我想把这两个值赋给自变量,比如$high_bid$high_bidder. 我知道这两个值在某种数组中。我读了抄本,但不确定如何访问和分配它们?

非常感谢您的帮助。我在这里学习:)

谢谢

2 个回复
SO网友:prosti

祝你学习好运。

$fivesdrafts = $wpdb->get_results( 
    "
    SELECT ID, post_title 
    FROM $wpdb->posts
    WHERE post_status = \'draft\' 
        AND post_author = 5
    "
);

foreach ( $fivesdrafts as $fivesdraft ) 
{
    echo $fivesdraft->post_title;
}
此示例由WordPress codex.

在你的情况下

$wpdb->get_results(
应该变成:

$variable_to_print = $wpdb->get_results(

SO网友:James

这就是成功的原因:

function find_highest_bid(){
    global $wpdb;
    $postid = get_the_id();
    $table = $wpdb->prefix . "jwp_bids";
    $highest_bid = $wpdb->get_results(
        "Select max(bid_amt) AS bid, bid_time, email
        FROM $table
        WHERE post_id = \'1277\'", ARRAY_A );
    $highest_bid = array_shift ( $highest_bid );
    $high_bid = $highest_bid[\'bid\'];
    $high_bidder = $highest_bid[\'email\'];
    $bid_time = $highest_bid[\'bid_time\'];
    echo \'$\'. $high_bid;
    echo ", ";
    echo $high_bidder;
    echo ", ";
    echo $bid_time;
}