Danielius
Danielius

Reputation: 837

Join doesnt work properly with WHERE

i have got a problem. I am trying to get ONLY users that last action was in 15 minutes. I am sure that my sql works when i dont use join(tried it), but when i add JOIN or LEFT, RIGHT or other JOINS I get all users, not only active in 15 minutes. Here is my code

function get_logged_users() {
global $dbh;
$users_info = $dbh->prepare(
    'SELECT
    users.username as `username`,
    users.id as `id`,
    ranks.style as `style`
    FROM `users` 
    LEFT JOIN `ranks` ON users.rank=ranks.id;
    WHERE users.last_login > ?');
$users_info->execute(array(date("Y-m-d H:i:s" ,strtotime( date("Y-m-d H:i:s")." -15 minutes" ))));
$users_info = $users_info->fetchAll();
return $users_info;

}

Can anyone help me? :) Thanks!

tables : users

ranks

Upvotes: 0

Views: 36

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

It's just a typo before the WHERE clause:

...
LEFT JOIN `ranks` ON users.rank=ranks.id;  <---
...

There should not be a semicolon (; points to the end of the query)

Upvotes: 3

atefth
atefth

Reputation: 1623

Try this sql -

$users_info = $dbh->prepare(
    'SELECT
    users.username as `username`,
    users.id as `id`,
    ranks.style as `style`
    FROM `users` 
    JOIN `ranks` ON ranks.id=users.rank
    WHERE users.last_login > ?');

Upvotes: 0

Related Questions