Selecting UserID's Database Entry

71 views Asked by At

I am using vBulletin forum software 3.8 in which there are multiple ways to pull the current users userid:

$vbulletin->GPC['userid']
$vbulletin->userinfo['userid']
$bbuserinfo['userid']

All of which return the same value, in my case 1

I have created a table to store login information which stores the users ID like so:

INSERT INTO logins (userid, phpdate)
VALUES (" . $vbulletin->userinfo['userid'] . ", " . TIMENOW . ")

Using Navicat I can run a query:

SELECT *
FROM logins
WHERE userid = 1
ORDER BY phpdate ASC
LIMIT 5

This gives me the correct results, so I then converted this query to vBulletin format changing the WHERE clause to:

WHERE userid = $vbulletin->userinfo['userid']

Doing this is throwing an error:

syntax error, unexpected 'userid' (T_STRING), expecting ')'

I have tried wrapping the variable in intval($var) IN ($var) but I can't seem to get any variation of this to work, I have even tried to escape the ' $vbulletin->userinfo[\'userid\'].

So question time: How can I select the userid within my query?

I am on PHP 7.4

To overcome this for now I am using an if condition in my while loop

while ($row = $result->fetch_assoc()){
    $row['phpdate'] =    vbdate( $vbulletin->options['dateformat'], $row['phpdate'], true );
    if ($vbulletin->userinfo['userid'] == $row['userid']){
        ...
    }
}

Although this is not allowing me to limit the results per userid, and I had other plans if I can get the SQL statement to function.

I am constantly messing with this waiting some help the current method I have is:

$result = $db->query('
SELECT *
FROM logins
WHERE userid = ' . $vbulletin->userinfo['userid'] . '
ORDER BY phpdate ASC
LIMIT 5
');
    while ($row = $result->fetch_assoc()){
        $row['phpdate'] =   vbdate( $vbulletin->options['dateformat'], $row['phpdate'], true );

     if ($vbulletin->userinfo['userid'] == $row['userid']){
      $drcpi_tbl_gutz .= '
        <li><ul class="device">
        <li class="icon">';
          $drcpi_tbl_gutz .= '<i class="fal fa-question-square fa-fw"></i>';
       $drcpi_tbl_gutz .= '</li>
        <li class="browser">' . $row['os'] . ' <small>(' . $row['browser'] . ')</small></li><li class="logindate time">' . $row['phpdate'] . '</li>
        <li class="ip-address">' . $row['ipaddress'] . ' <a class="location-lookup" title="Get approximate location of this IP address." href="https://www.iplocation.net/?query=' . $row['ipaddress'] . '"><i class="fal fa-location-arrow"></i></a></li></ul></li>
     ';
      }
    }
    $result->free();

Which is throwing an invalid SQL error:

WHERE userid = ' . Array['userid'] . '
1

There are 1 answers

1
DrCustUmz On

After multiple attempts at figuring this out I decided to look at some other vBulletin products. Following suit with them I wrote my query how they appeared in other products.

My end result was:

$result = $db->query("
SELECT *
FROM logins
WHERE userid='".$vbulletin->userinfo['userid']."'
");

The difference being the types of quotes, and where they were placed.

If anyone would care to elaborate on why this solution works when the method provided in the original post does not, I would appreciate the feedback. All I know is this way works and that way did not.