WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying Database Data on a Page based on User (3 posts)

  1. thisistrav
    Member
    Posted 2 years ago #

    Hi There,

    I've been spending waaaay too much time trying to figure this out on my own. Partly because I'm stubborn, party because it sounds like it should be relatively easy.

    I'm simply trying to display the value of a MSSQL DB field depending on the current user signed in.

    E.g. A customer, Bob, has a reference number of ABC123. Therefore, when user 'bob' logs in I want to be able to display 'ABC123' on a page (not profile).

    I searched everywhere for days but just can't get it to work all together. I'd appreciate any guidance.

    Many thanks in advance.

  2. thisistrav
    Member
    Posted 2 years ago #

    OK, so all I apparently needed was another half a day.

    I used the plugin: Shortcode Exec PHP, then created a shortcode for the below code:

    global $wpdb;
    global $current_user;
    $current_user = wp_get_current_user();
    return $wpdb->get_var("SELECT field FROM table WHERE field = '$current_user->user_login'");

    ... with "output echoed" unticked.

    The shortcode then easily placed in a page.

    I believe I was close on a number of occasions way back, but had the quotes mixed up on the $current_user->user_login variable.

    Glad that's over. Next challenge now. Hope that might help someone else.

  3. thisistrav
    Member
    Posted 2 years ago #

    Sorry, testing code up there ^.

    With SQL injection protection on... last line reads...

    return $wpdb->get_var($wpdb->prepare("SELECT field FROM table WHERE field = '$current_user->user_login'"));

Topic Closed

This topic has been closed to new replies.

About this Topic