tags:

views:

53

answers:

1

I wanna be able to get everything from a signed in user. Like, username, email, msn, and alto more with a function like:

echo getUserInfo('email');

gets the content of "email". but it does not work, see below:

My function:

function getUserInfo($field) {

global $userID;

    $sql = mysql_query("select * from users where id = $userID");
    mysql_result($sql, 0, "$field");

   return $field;

}

Does not work, it outputs whatever i put in the function when i call it

+1  A: 

edit fixed it:

$userID = 1;

function getUserInfo($field) {

global $userID;

    $sql = mysql_query("select * from users where id = $userID");
    $pop = mysql_result($sql, 0, "$field");

   return $pop;

}
Jamal
hm must wait 2 days before i can mark it accepted
Jamal
you could just `select $field from users ...`
nickf
This is to discourage people from asking and then immediately answering, and to give others a fair chance at answering the question.
Chacha102
note: you should probably define the function as `getUserInfo($userID,$field)` to avoid the global variable.
Piskvor
I think you should go search Global Variables on StackOVerflow and look at the overwhelming number of places people advocate their evilness.
Chacha102
By the way, this is a very bad practice imho. To get one field you select all the data, at least you can do "select $field...".Still it is bad, because everytime you need something you send a request to db, instead of taking all at one time and using whereever you want.Say you have a user page, where you want to output some user data, you can just use getUser($userId) { return $user; } // selects all user and return it as an object or array. Then you can use it whereever you want. This is good for db.
marvin
I need to use this all over the site not just on profile
Jamal
Huh? None of these suggestions would prevent you from using it all over the site.
ceejayoz
Could you post an example marvin?
Jamal