wpdb usage

This topic is: resolved

This topic contains 2 replies, has 2 voices, and was last updated by  phamhung 2 years, 7 months ago.

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


    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.



    Kim Parsell


    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:

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

    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:

    $usersarray = get_userdata(5);
    $useremail = $user_info->user_email;
    echo "$useremail";

    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



    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.