PDA

View Full Version : How do I get the userid from the username?


Quarterbore
05-06-2006, 10:04 PM
I have a script that I want to be able to convert an entered username (in $username) to the userid (in $userid).

I found an old thread for vb 3.0 here:

https://vborg.vbsupport.ru/showthread.php?t=61558

I tried to use the code suggested in there as follows:



$getuser = $vbulletin->db->query("
SELECT userid,username
FROM myuser
WHERE username = $username
");
$user = $DB_site->fetch_array($getuser);
echo("The userid for $user[username] is <b>$user[userid]</b>");


I know my post is getting the correct $username into this but I am getting a MYSQL error that says...


Database error in vBulletin 3.5.2:

Invalid SQL:

SELECT userid,username
FROM XXXXXXXXXX
WHERE username = Quarterbore;

MySQL Error : Unknown column 'Quarterbore' in 'where clause'
Error Number : 1054
Date : Saturday, May 6th 2006 @ 05:17:49 PM
Script : http://www.XXXXXXXXXX.com/XXXXXXXXXX.php
Referrer : http://www.XXXXXXXXXX.com/XXXXXXXXXX.php?do=ad&id=50
IP Address : XXX.XXX.XXXX
Username : Quarterbore
Classname : vb_database


I just can't finish my minor addition until I can get this basic change of $username into $userid

I really do appreciate any help as I have tried all kinds of combinations for the past 4-hrs or more and I am just stumped...

derekivey
05-07-2006, 01:44 AM
You could try this:


$getuser = $db->query_read("SELECT userid FROM user WHERE username='".$db->escape_string($username)."'");
$user = $db->fetch_array($getuser);
echo("The userid for $username is <b>$user[userid]</b>");

Quarterbore
05-07-2006, 12:02 PM
Thank You!!! That worked... back to my hack!

:banana: ;) :banana:

derekivey
05-07-2006, 03:42 PM
No Problem :)

Quarterbore
05-11-2006, 04:14 PM
You could try this:


$getuser = $db->query_read("SELECT userid FROM user WHERE username='".$db->escape_string($username)."'");
$user = $db->fetch_array($getuser);
echo("The userid for $username is <b>$user[userid]</b>");


If I may ask for more help, how can I put a reality check into this to verify that the value in $username matches something that is in the database? As it is now, if there is a typo the server generates a MYSQL error.

derekivey
05-11-2006, 06:42 PM
$getuser = $db->query_read("SELECT userid FROM user WHERE username='".$db->escape_string($username)."'");
$num_users = $db->num_rows($getuser);

if ($num_users == 1)
{
$user = $db->fetch_array($getuser);
echo("The userid for $username is <b>$user[userid]</b>");
}
else
{
echo("The Username Does Not Exist");
}


Try that, let me know if you have any problems.

Thanks,

Derek