【问题标题】:getting specific data from wordpress database table从 wordpress 数据库表中获取特定数据
【发布时间】:2013-09-02 11:05:42
【问题描述】:

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

该表有一个名为transaction_user_ID 的列和另一个名为transaction_paypal_ID 的列。

逻辑:如果用户 id == 交易用户 id 则获取交易 paypal 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 ';}?>

【问题讨论】:

  • 这段代码甚至不会运行。你有至少可以运行的代码吗?这是一个相当大的混乱。
  • 不,我没有,我仍在尝试正确获取代码以至少运行代码,但我仍在从教程中学习

标签: php database wordpress


【解决方案1】:

此代码没有根据,因为您没有提供数据库结构、字段名称或许多其他因素。

但是,将其视为伪代码,这将使您朝着正确的方向前进。

$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.';
}

【讨论】:

    【解决方案2】:

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

    其次,你的查询是错误的;你没有关闭引号,并且有一个杂散的分号。如果我没看错,你需要这样的东西:

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

    如果您在查询中只获得一个值,您可以使用 $wpdb-&gt;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';
    }
    ?>
    

    【讨论】:

    • 谢谢,这完全符合我的意愿,这是我可以掩盖我建立的网址的一种方式,因为它可以减轻网址中的一些密码
    【解决方案3】:

    试试这个查询。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2021-10-15
      • 2021-10-25
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多