user12927558
user12927558

Reputation:

Count live online players

I have a playable client and I want to echo/show my online users on my index page. In my database I have the table settings and in that 2 rows setting and value. e.g. players.online and value 0 as standard counting up to every number how much players there are online. But, it's not counting. The counter stays on 0.

function onlineusers(){
    $link = mysqli_connect("xxx", "xxx", "xxx" "`xxx");
    mysqli_query($link, "SELECT value FROM settings WHERE value = 'players.online'");
    $i = 0;
    while($b = mysqli_fetch_array($link)){
        if(onlineusers($b['players.online'])){
            $i++;
        }
    }
    return $i;
}

Upvotes: 1

Views: 211

Answers (2)

Josh
Josh

Reputation: 62

Should be able to just run a COUNT() in your query instead of counting them up in PHP.

SELECT COUNT(*) as NumOnline FROM settings WHERE value = 'players.online';

Upvotes: 1

Ankur Mishra
Ankur Mishra

Reputation: 1296

You can count the number of rows in DB and return it.

function onlineusers(){
    $link = mysqli_connect("xxx", "xxx", "xxx" "`xxx");
    $result = mysqli_query($link, "SELECT count(*) as total FROM settings WHERE value = 'players.online'");

    $data=mysql_fetch_assoc($result);
    return $data['total'];
}

Upvotes: 4

Related Questions