wpdb usage

This topic is: resolved

This topic contains 2 replies, has 2 voices, and was last updated by  phamhung 1 year, 6 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #38154

    phamhung
    Participant
    Post count: 31

    Here is the code

    $usersarray = $wpdb->get_results( “SELECT user_email FROM $wpdb->users WHERE id=’5′” );
    echo $usersarray->user_email;

    It returns NULL. I have “global  $wpdb” already, but not sure why it could not take data? Surely, I have an user with id=”5″ in users table.

    Please suggest.

    Thanks.

    #38191

    Kim Parsell
    Participant
    Post count: 17

    @phamhung:

    First, double-check that you have the user ID correct. If you have deleted any users, then user 5 may not exist.

    Try this code and see if it works:

    [php]
    $usersarray = $wpdb->get_results( "SELECT user_email FROM $wpdb->users WHERE ID=5" );
    echo $usersarray->user_email;
    [/php]

    More info on SELECT statements in $wpdb and examples can be found in the Codex: Codex.

    The preferred way to get this information is to use the function get_userdata:

    [php]
    $usersarray = get_userdata(5);
    $useremail = $user_info->user_email;
    echo "$useremail";
    [/php]

    There are so many existing functions in WordPress that will retrieve data for you rather than having to do a $wpdb query. Check the Function Reference for the user and author functions.

    Hope this helps. :)


    Just a simple country girl | Twitter

    #38339

    phamhung
    Participant
    Post count: 31

    The user id is correct. Also, the code is correct. The wrong thing is my “middle” code before echo. I found out and fixed already.

    Thanks for replying anyway.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘wpdb usage’ is closed to new replies.