从wordpress数据库表中获取特定数据


getting specific data from wordpress database table

我正在尝试使用WordPress登录的用户id,该用户id应在表wp_m_subscription_transaction中搜索transaction_user_ID

该表有一个列称为transaction_user_ID,另一个列名为transaction_paypal_ID

逻辑:如果用户id==交易用户id,那么获得交易贝宝id

应该将其与用户id电子邮件地址一起传递到url执行url以获得代码-这是我的最终输出

如何做到这一点?

我正在开发的代码是这样的,显然是不完整的&获取和使用数据库查询的错误

<?php $user_id = get_current_user_id();
       $query = "SELECT * FROM $wpdb->;wp_m_subscription_transaction 
       if ($user_id ==$query) {
         <a href="http://primary.copyminder.com/cm_prodkey.php?DID=********&Password=*****&ProdCode=*******&NumRequired=1&TransNum=(GET THIS FROM DATABASE) &CustEmail=". $current_user->user_email .">Get ur Code</a>
   } 
else {
echo 'You are not logged in as user ';}?>

首先,由于您也需要电子邮件地址,请使用wp_get_current_user()获取当前用户的所有详细信息,而不仅仅是id。

其次,您的查询是错误的;你还没有结束引号,并且有一个不正确的分号。如果我没看错,你需要这样的东西:

select transaction_paypal_ID
  from wp_m_subscription_transaction
 where transaction_user_ID = [the user's ID]

如果您在查询中只得到一个值,则可以使用$wpdb->get_var() 检索它

如果查询没有返回一行,并不一定意味着用户没有登录。您可以使用is_user_logged_in()检查用户是否登录。

这样的东西应该行得通。我还没有测试,你必须自己建立URL。

<?php
if (is_user_logged_in()) {
    $user = wp_get_current_user();
    $paypal_id = $wpdb->get_var("select transaction_paypal_ID from wp_m_subscription_transaction where transaction_user_ID = " . (int) $user->ID);
    if ($paypal_id) {
        // Build up your URL here, with $paypal_id and $user->user_email
    }
    else {
        echo 'No transaction found for the current user';
    }
}
else {
    echo 'You are not logged in';
}
?>

尝试此查询。

$query = "SELECT transaction_paypal_ID FROM wp_m_subscription_transaction where transaction_user_ID = ".$user_id;
$result = $wpdb->get_row($wpdb->prepare($query), ARRAY_A);
$transaction_paypal_ID = $result['transaction_paypal_ID'];

由于您没有提供数据库结构、字段名或许多其他因素,因此此代码没有任何保证。

然而,作为伪代码,这将使您朝着正确的方向前进。

$user_id = get_current_user_id();
// Global / bring in $wpdb, and the $table_prefix variables
global $wpdb, $table_prefix;
// Use $wpdb prepare to be sure the query is properly escaped
$query = $wpdb->prepare("SELECT * FROM " . $table_prefix . "m_subscription_transaction WHERE user_id = %d", $user_id);
// Turn on error reporting, so you can see if there's an issue with the query
$wpdb->show_errors();
// Execute the query
$results = $wpdb->get_results($query);
// Are there any records?  If so, that means the user ID matched
if ($results) {
    $row = $results[0];
    // Get the data from the row
    echo '<a href="http://primary.copyminder.com/cm_prodkey.php?DID=********&Password=*****&ProdCode=*******&NumRequired=1&TransNum=' . $row->TransNum . ' &CustEmail=". $current_user->user_email .">Get ur Code</a>';
} else {
    echo 'No records for this user.';
}