PDA

View Full Version : Select count from table...


sixpackspeak
11-11-2016, 01:05 PM
I am using this code for an AJAX Chat Integration.

https://vborg.vbsupport.ru/showthread.php?t=269509&page=2

Updated to the following recently:
vBulletin 4.2.3 Patch Level 2
PHP 5.6.27-0+deb8u1 (cli)

And now the user counter doesn't work properly on the nav bar.

FYI, I've updated the "global_bootstrap_init_start" slightly, to make it a little more specific (and to remove duplicate login entries):
$resCNT = mysql_query("SELECT COUNT(userID) FROM ajax_chat_online GROUP BY userID");
$num_chatting = $resCNT[0];
vB_Template::preRegister('navbar',array('num_chatt ing' => $num_chatting));
vB_Template::preRegister('FORUMHOME',array('num_ch atting' => $num_chatting));

The query works perfectly as intended, however, it was not outputting any numerical counts, either with the current code (in OP) or the updated code. Does anyone have any suggestions?

Dave
11-11-2016, 01:42 PM
It's not outputting anything because you don't fetch the results.
What you want to do is COUNT all DISTINCT records, example (untested):

$resCNT = $vbulletin->db->query_first_slave("SELECT COUNT(DISTINCT userID) AS online FROM " . TABLE_PREFIX . "ajax_chat_online");
$num_chatting = $resCNT['online'];
vB_Template::preRegister('navbar',array('num_chatt ing' => $num_chatting));
vB_Template::preRegister('FORUMHOME',array('num_ch atting' => $num_chatting));

sixpackspeak
11-11-2016, 02:33 PM
Thank you. That did it.