• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Echo in array for total count in xf_session


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.

        $query_count = 'SELECT COUNT(*) as total FROM `xf_session`';
        $query_count_total = XenForo_Application::get( 'db' )->fetchAll( $query_count );
This gives the array in the array ...

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.
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?

Chris D

XenForo developer
Staff member
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:

 array(1) {
  [0] => array(1) {
    ["total"] => int(1)
So you would use:
echo $query_count_total;
Instead of:
echo $query_count_total[0]['total'];