PDA

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


killervette
02-14-2004, 08:27 PM
What is the easiest way to get a userid based on the username in php script? thanks.

assassingod
02-14-2004, 08:29 PM
The query would be:

SELECT userid
FROM user
WHERE username = 'xxx'


xxx being the username

MindTrix
02-14-2004, 08:29 PM
Could you explain a bit more please mate?

MindTrix
02-14-2004, 08:30 PM
ah ok :)

killervette
02-14-2004, 08:46 PM
How do you connect to the database??

assassingod
02-14-2004, 08:50 PM
Use a script like this:

<?php
error_reporting(E_ALL & ~E_NOTICE);
require_once('./global.php');
$getuser = $DB_site->query("
SELECT userid,username
FROM user
WHERE username = 'admin'
");
$user = $DB_site->fetch_array($getuser);
echo("The userid for $user[username] is <b>$user[userid]</b>");
?>



xxx being the username

Andreas
02-14-2004, 08:51 PM
$link = mysql_connect('host', 'username', 'password') or die('Could not connect to mySQL-server');
mysql_select_db('vbdb', $link) or die('Could not select vBulletin database');


'host' and so on must be replaced by the actual values of course.

killervette
02-14-2004, 09:00 PM
neither worked??

assassingod
02-14-2004, 09:01 PM
Save mine as script.php and upload it to your forums directory and then go youforums.com/script.php. It should work

killervette
02-14-2004, 09:04 PM
This page is not gonna be in the forum directory.

killervette
02-14-2004, 09:05 PM
The result for the script echos this "The userid for is ". I specify a username that exists case sensitive.

assassingod
02-14-2004, 09:06 PM
It should still work if you replace

require("./global.php");


with


chdir("YOUR forum directoru");
require("./global.php");

assassingod
02-14-2004, 09:10 PM
The result for the script echos this "The userid for is ". I specify a username that exists case sensitive.
Recheck my script, I missed out a $. It works perfectly now, ive tested it:)

Sorry!:)

killervette
02-14-2004, 10:15 PM
I put

chdir("/mainforum/");
require("./global.php");

i have the file in the root and my directory is mainforum

Andreas
02-14-2004, 10:18 PM
The directory is REALLY /mainforum?
I doubt that ... except you are in a chroot-jail.
More likely it is smt.h like /var/htdocs/youraccount/html/mainforum

Quarterbore
05-06-2006, 09:53 PM
I have a situation where I also need to convert a USERNAME from a template to a USERID inside a main php file....

I tried to use the code above in my file like this:


$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 would really appreciate any help!

bairy
05-10-2006, 07:39 PM
you can use
$user = $vbulletin->db->query_first("SELECT userid FROM user WHERE username = '$username'
(You were missing the ' to surround $username, but query_first does it all in one for you anyway.)

$user['userid'] will then contain the userid

Marco van Herwaarden
05-16-2006, 08:45 AM
Can we please keep vB3.5 questions and solutions in the vB3.5 forum. This to avoid confusion.