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.
Are you a blogger, web designer, developer, or website owner looking to generate more income?
Promote products for the largest and fastest growing online website platform as part of our affiliate program.