MyBB Community Forums

Full Version: Get User ID from username.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How would I use the fetch_field function or some other database function to get the User ID of a member based on the username entered? It seems it would be one line of code, so if someone can contribute this to me, please do so! Thank you! Smile
This is how I do it in my plugin development:

$user = 'My User';
$query = $db->simple_select("users","*","username='$user'");
$uid = $db->fetch_field($query, "uid");
Thanks! Definitely a Reputation + 1 for you! Big Grin
Be sure to sanitize any user input. If this is coming from a field, use this:

$user = $db->escape_string($mybb->input['user']);
$query = $db->simple_select("users","*","username='$user'");
$uid = $db->fetch_field($query, "uid");
WHAT Means by coming from filed?
(2012-11-10, 06:50 AM)WINBOY Wrote: [ -> ]WHAT Means by coming from filed?
Can you be more specific please? Couldn't get you.

And anyway, one can use get_user function.
what will be difference after running query from paul and naxus.
will it change profile link to www.xyz.com/user-1.html???
If the user is inputting something into a field (like how I am typing this in a text area), make sure to sanitize it or your plugin/forum will be vulnerable to injections like SQL Injections.

Edit: In response to your question: Paul's is more secure. It won't change the URL.
The following will work with lower/upper letters:
$query = $db->simple_select('users', '*', 'LOWER(username)=\''.$db->escape_string(my_strtolower($mybb->input['user'])).'\'');
$uid = $db->fetch_field($query, 'uid'); 
You're making it complex, unless I am understanding different.

You can just grab input username and derive various other values from it including uid using get_user function, which already does the query job.

$username = $db->escape_string($mybb->input['username']);
$user = get_user($username);
$uid = $user['uid']; //Similarly you can derive all values from user table, like $user['ipaddress'], etc
Pages: 1 2