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

Php 从wordpress数据库表获取特定数据,php,database,wordpress,Php,Database,Wordpress,我正在尝试使用WordPress登录用户id,它应该在表wp\u m\u subscription\u transaction中搜索transaction\u user\u id 该表有一列名为transaction\u user\u ID,另一列名为transaction\u paypal\u ID 逻辑:如果用户id==交易用户id,则获取交易贝宝id 应将其与用户id电子邮件地址一起传递到url 然后执行url以获取代码-这是我的最终输出 如何做到这一点 我正在开发的代码是这样的,显然是不

我正在尝试使用WordPress登录用户id,它应该在表
wp\u m\u subscription\u transaction
中搜索
transaction\u user\u id

该表有一列名为
transaction\u user\u ID
,另一列名为
transaction\u paypal\u 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 ';}?>
尝试此查询

$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'];
尝试此查询

$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'];

首先,因为您还需要电子邮件地址,所以使用
wp\u get\u current\u user()
获取当前用户的所有详细信息,而不仅仅是id

第二,你的疑问是错误的;您还没有关闭引号,并且有一个零散的分号。如果我没看错的话,你需要这样的东西:

select transaction_paypal_ID
  from wp_m_subscription_transaction
 where transaction_user_ID = [the user's ID]
如果在查询中只获取单个值,可以使用
$wpdb->get_var()

如果查询未返回行,则不一定意味着用户未登录。您可以使用
is\u user\u logged\u 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';
}
?>

首先,由于您还需要电子邮件地址,请使用
wp\u get\u current\u user()
获取当前用户的所有详细信息,而不仅仅是id

第二,你的疑问是错误的;您还没有关闭引号,并且有一个零散的分号。如果我没看错的话,你需要这样的东西:

select transaction_paypal_ID
  from wp_m_subscription_transaction
 where transaction_user_ID = [the user's ID]
如果在查询中只获取单个值,可以使用
$wpdb->get_var()

如果查询未返回行,则不一定意味着用户未登录。您可以使用
is\u user\u logged\u 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';
}
?>

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

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

$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.';
}
$user\u id=get\u current\u user\u id();
//全局/引入$wpdb和$table_前缀变量
全局$wpdb,$table_前缀;
//使用$wpdb prepare确保正确转义查询
$query=$wpdb->prepare(“从“$table\u prefix.”m\u subscription\u事务中选择*,其中user\u id=%d“,$user\u id”);
//启用错误报告,以便查看查询是否存在问题
$wpdb->show_errors();
//执行查询
$results=$wpdb->get_results($query);
//有记录吗?如果是,则表示用户ID匹配
如果(结果){
$row=$results[0];
//从行中获取数据
回声';
}否则{
回显“此用户没有记录”;
}

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

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

$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.';
}
$user\u id=get\u current\u user\u id();
//全局/引入$wpdb和$table_前缀变量
全局$wpdb,$table_前缀;
//使用$wpdb prepare确保正确转义查询
$query=$wpdb->prepare(“从“$table\u prefix.”m\u subscription\u事务中选择*,其中user\u id=%d“,$user\u id”);
//启用错误报告,以便查看查询是否存在问题
$wpdb->show_errors();
//执行查询
$results=$wpdb->get_results($query);
//有记录吗?如果是,则表示用户ID匹配
如果(结果){
$row=$results[0];
//从行中获取数据
回声';
}否则{
回显“此用户没有记录”;
}

此代码甚至无法运行。你有至少可以运行的代码吗?这是一个相当大的混乱。不,我没有它,我仍然试图得到正确的代码,使代码至少运行,但我仍然从教程中学习这段代码甚至不会运行。你有至少可以运行的代码吗?这是一个相当大的混乱。不,我没有它,我仍然试图得到正确的代码,使代码至少运行,但我仍然从教程中学习感谢这完全符合我的愿望,是他们的一种方式,我可以掩盖我建立的url,因为它将解除一些密码在url感谢这完全符合我的愿望,是他们的一种方式,我可以掩盖我建立的网址,因为它将释放一些密码在网址