Here is the code
$usersarray = $wpdb->get_results( "SELECT user_email FROM $wpdb->users WHERE id='5'" );
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.
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.
The topic ‘wpdb usage’ is closed to new replies.
Subscribe to get early access to new themes, discounts and brief updates about what's new with StudioPress!