How to Simple Select refferer id from users table in Database .
#1
I'm trying to select the User ID of the referrer of the currently logged in user. something like...


$query = $db->simple_select("users", "uid,username" , 'referrer');

 $referrer = $db->fetch_array($query);

So the
$referrer = 1
if user was reffered by first
Reply
#2
Untested, but you probably want something like this:

$query = $db->simple_select('users', 'referrer', "id={$mybb->user['uid']}");
$referrer = $db->fetch_field($query, 'referrer');

Be prepared for $referrer to be null or otherwise empty. Also, you should probably ensure that there is a logged in user before calling this code, i.e., that $mybb->user['uid'] != 0.
Reply
#3
It's generating an SQL error.
Reply
#4
Please share the error message.

Oh, wait: id should be uid in my (untested) code. So, the code should be:

$query = $db->simple_select('users', 'referrer', "uid={$mybb->user['uid']}");
$referrer = $db->fetch_field($query, 'referrer');
Reply
#5
It works fine now, Thank you.
Reply
#6
We dont need query to do this
My Plugins

PM only paid support
Reply
#7
(2021-02-08, 04:48 PM)Supryk Wrote: We dont need query to do this

As Supryk said, no need to do a query.

$mybb->user['referrer'];

You only need to query if it is for another user than the logged in user.
Reply
#8
You guys are right. I had a lapse there.
Reply
#9
As a general rule, anytime you need data about the current user you can use $mybb->user['fieldname'] and if you need usergroup permissions you use $mybb->usergroup['permission_name'].
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)