getting specific data from wordpress database tabl

2019-09-01 08:26发布

I am trying to use WordPress logged in user id which should search transaction_user_ID in the table wp_m_subscription_transaction.

The table has a column called transaction_user_ID and another called transaction_paypal_ID.

logic:if user id == transaction user id then get transaction paypal id

which should be passed to the url along with the user id email address and the execute the url to get the a code - this is my final output

How this can be achieved?

The code which I am developing is like this which is obviously incomplete & error for the getting and using the database query

<?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 ';}?>

3条回答
劳资没心,怎么记你
2楼-- · 2019-09-01 08:56

Try this query.

$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'];
查看更多
3楼-- · 2019-09-01 09:00

First, since you need the email address as well, get all the current user's details, not just the id, using wp_get_current_user().

Secondly, your query is wrong; you haven't closed the quotes, and have a stray semicolon. If I've read it right, you need something like:

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

If you're only getting a single value in a query, you can retrieve it with $wpdb->get_var()

If the query doesn't return a row, it doesn't necessarily mean the user isn't logged in. You can check if a user is logged in using is_user_logged_in().

Something like this should work. I haven't tested, and you'll have to build up the URL yourself.

<?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';
}
?>
查看更多
Viruses.
4楼-- · 2019-09-01 09:04

This code comes without warrant, because you've not provided database structure, field names, or many other factors.

However, treated as pseudo-code, this will get you going in the right direction.

$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.';
}
查看更多
登录 后发表回答