Echo in array for total count in xf_session

LPH

Well-known member
This is going to read as "a really stupid question." I apologize because my eyes are blurry and my mind is super numb. Regardless, would someone please look at the following code and nudge me in the right direction.

Code:
        $query_count = 'SELECT COUNT(*) as total FROM `xf_session`';
        $query_count_total = XenForo_Application::get( 'db' )->fetchAll( $query_count );
        print_r($query_count_total);

This gives the array in the array ...

Code:
Array ( [0] => Array ( [total] => 3 ) )

Now, I'm stuck on the echo statement. The answer is staring me in the face (the print_r) but the head smack isn't there :)

I realize this is wrong.
Code:
echo $query_count_total['total'];

So, what is the best way to return that "total" ?

And - while you are smacking me for being silly - is there a better way to return the number of online guests? Did I miss a function already in XenForo?
 
You can improve this somewhat.

Instead of using "fetchAll" use "fetchOne".

Familiarise yourself with the various fetch functions. They perform the query for you then format the results in a specific way. fetchAll will always return an array for each row found. If you were to use fetchOne you no longer need the "as total" declaration and it will return the first value in the first column of the first row:
Code:
int(1)

vs:

Code:
 array(1) {
  [0] => array(1) {
    ["total"] => int(1)
  }
}

So you would use:
PHP:
echo $query_count_total;

Instead of:
PHP:
echo $query_count_total[0]['total'];
 
  • Like
Reactions: LPH
Top Bottom