$wpdb->get_var isn't working for me

白昼怎懂夜的黑 提交于 2019-12-11 05:31:43

问题


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.



来源:https://stackoverflow.com/questions/16298739/wpdb-get-var-isnt-working-for-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!