$wpdb->get_var isn't working for me

2019-08-02 09:19发布

问题:

I am working on a paypal IPN handler that gets an IPN then sends an e-mail. Right now I am testing it trying to access the 'paid' column of my database to make sure the transaction has not already previously been processed. However $wpdb doesn't seem to be returning things correctly. I was able to use $wpdb on another form to insert things into the database but I am having trouble getting it back out. I have tried get_var, get_row, and get_results and tried to access them as objects, arrays, or a variable in the case of get_var.

$paidstatus = $wpdb->get_var("SELECT paid FROM wp_bbbiller WHERE txn_id = '$txn_id'");

$subject = 'Test e-mail';
$message = "Thank you for your purchase $payer_email. Your total was $".$payment_amount."     for $duration hour 
    with $usercount users.  Previously paid: $paidstatus";

mail ($payer_email , $subject , $message);

Right now the $paidstatus shows up blank in the e-mail. I tried using print_r($paidstatus) when I had it setup with get_row and it showed up as a '1' in the email, even though the database value is 0.

回答1:

Yes using get_results works! I was able to access the variable like this

$paidstatus = $wpdb->get_results("SELECT paid FROM wp_bbbiller WHERE txn_id = '$txn_id'");

foreach ($paidstatus as $status) {
    $subject = 'Test e-mail';
    $message = "Thank you for your purchase $payer_email. Your total was $".$payment_amount."          for $duration hour 
    with $usercount users.  Previously paid: ".$status->paid;

    mail ($payer_email , $subject , $message);

}

Alternatively get_row also works like this

    $paidstatus = $wpdb->get_row("SELECT paid FROM wp_bbbiller WHERE txn_id = '$txn_id'");

    $subject = 'Test e-mail';
    $message = "Thank you for your purchase $payer_email. Your total was $".$payment_amount." for $duration hour 
    with $usercount users.  Previously paid: ".$paidstatus->paid;

    mail ($payer_email , $subject , $message);


回答2:

I think you need to use get_results. Then use a while loop to set your status from the database as a variable for insertion into the email.

Also what do you want it to say in your message where you currently have the variable $paidstatus. You wouldn't want a 0 or 1 like your database is storing would you? You need to create an if statement to set the value of the variable depending on its status in your database.