Mysql Online Buddy Count

Started on the old forum in April 2008 · view the original in the archive · old links redirect here

9 replies · 1,004 views · started 18 years ago · 1 min read

3 participants
GumSlone Administrator Principal · 1,690 posts #1

Hi, im trying to create a different buddylist as i had before so id like to create a working online buddy counter which does not work for me well, it gives me as result the number of all buddies i got and i need only online buddies.

Here is a code:

$onbuddies=mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT buddies.id) FROM buddies JOIN online ON (buddies.buddy1 = online.user_id) OR (buddies.buddy2 = online.user_id) WHERE ((buddies.buddy1 = '".$user_id."') OR (buddies.buddy2 = '".$user_id."')) AND buddies.accepted=1 AND online.lastactive > 0"));

Thanks.

Here is a way which works for me but its not nice coding.

$sql=(mysql_query("(SELECT buddies.buddy2 AS buddy, online.lastactive FROM buddies JOIN online ON buddies.buddy2=online.user_id WHERE buddies.buddy1='".$user_id."' AND buddies.accepted='1')
          UNION ALL (SELECT buddies.buddy1 AS buddy, online.lastactive FROM buddies JOIN online ON buddies.buddy1=online.user_id WHERE buddies.buddy2='".$user_id."' AND buddies.accepted='1')"));
echo mysql_num_rows($sql).' online buddies';

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

GumSlone Administrator Principal · 1,690 posts #3
CREATE TABLE `mysite_buddies` (
  `id` int(11) NOT NULL auto_increment,
  `buddy1` int(11) NOT NULL default '0',
  `buddy2` int(11) NOT NULL default '0',
  `accepted` int(11) NOT NULL default '0',
  `asked` int(11) NOT NULL default '0',
  `time` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

CREATE TABLE `mysite_online` (
  `user_id` int(11) unsigned NOT NULL default '0',
  `location` varchar(50) collate latin1_general_ci NOT NULL default '',
  `lastactive` int(11) NOT NULL default '0',
  PRIMARY KEY  (`user_id`),
  UNIQUE KEY `user_id` (`user_id`),
  KEY `location` (`location`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

CREATE TABLE `mysite_users` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(50) NOT NULL default '',
  `password` varchar(50) NOT NULL default '',
  PRIMARY KEY  (`id`),
  KEY `username` (`username`),
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

rukiya Lead · 569 posts #4

Hi, im trying to create a different buddylist as i had before so id like to create a working online buddy counter which does not work for me well, it gives me as result the number of all buddies i got and i need only online buddies.

Here is a code:

$onbuddies=mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT buddies.id) FROM buddies JOIN online ON (buddies.buddy1 = online.user_id) OR (buddies.buddy2 = online.user_id) WHERE ((buddies.buddy1 = '".$user_id."') OR (buddies.buddy2 = '".$user_id."')) AND buddies.accepted=1 AND online.lastactive > 0"));

Thanks. Here is a way which works for me but its not nice coding.

$sql=(mysql_query("(SELECT buddies.buddy2 AS buddy, online.lastactive FROM buddies JOIN online ON buddies.buddy2=online.user_id WHERE buddies.buddy1='".$user_id."' AND buddies.accepted='1')
          UNION ALL (SELECT buddies.buddy1 AS buddy, online.lastactive FROM buddies JOIN online ON buddies.buddy1=online.user_id WHERE buddies.buddy2='".$user_id."' AND buddies.accepted='1')"));
echo mysql_num_rows($sql).' online buddies';

I wonder that none gave you a fixed code anyway here is the fixed code that you wanted

$onbuddies=mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT online.user_id) FROM buddies JOIN online ON (buddies.buddy1 = online.user_id) OR (buddies.buddy2 = online.user_id) WHERE (buddies.buddy1 = '".$user_id."' OR buddies.buddy2 = '".$user_id."') AND buddies.accepted=1 AND online.lastactive > 0"));
rukiya Lead · 569 posts #7

Thanks for trying to help but the code is not working

No its working.I just tested it again and here are the data that I used to test user no 1's buddies count

-- phpMyAdmin SQL Dump
-- version 2.10.1
-- [url]http://www.phpmyadmin.net[/url]
-- 
-- Host: localhost
-- Generation Time: Jun 15, 2008 at 06:28 PM
-- Server version: 5.0.41
-- PHP Version: 5.2.2

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

-- 
-- Database: `test`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `buddies`
-- 

CREATE TABLE `buddies` (
  `id` int(11) NOT NULL auto_increment,
  `buddy1` int(11) NOT NULL default '0',
  `buddy2` int(11) NOT NULL default '0',
  `accepted` int(11) NOT NULL default '0',
  `asked` int(11) NOT NULL default '0',
  `time` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4;

-- 
-- Dumping data for table `buddies`
-- 

INSERT INTO `buddies` VALUES (1, 1, 2, 1, 0, 0);
INSERT INTO `buddies` VALUES (2, 1, 3, 1, 0, 0);
INSERT INTO `buddies` VALUES (3, 4, 1, 1, 0, 0);

-- 
-- Table structure for table `online`
-- 

CREATE TABLE `online` (
  `user_id` int(11) unsigned NOT NULL default '0',
  `location` varchar(50) collate latin1_general_ci NOT NULL default '',
  `lastactive` int(11) NOT NULL default '0',
  PRIMARY KEY  (`user_id`),
  UNIQUE KEY `user_id` (`user_id`),
  KEY `location` (`location`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

-- 
-- Dumping data for table `online`
-- 

INSERT INTO `online` VALUES (1, 'main menu', 1);
INSERT INTO `online` VALUES (4, 'forum', 1);
INSERT INTO `online` VALUES (5, 'main menu', 1);
INSERT INTO `online` VALUES (6, 'chat', 1);

and the mysql query I used

SELECT COUNT( DISTINCT online.user_id ) 
FROM buddies
JOIN online ON ( buddies.buddy1 = online.user_id ) 
OR (
buddies.buddy2 = online.user_id
)
WHERE (
buddies.buddy1 = '1'
OR buddies.buddy2 = '1'
)
AND buddies.accepted =1
AND online.lastactive >0

and query result is

COUNT( DISTINCT online.user_id ) 
2

because 1 &4 user were online where the buddies are 1,2,3,4 though according to code the user itself is counted as a buddy also

GumSlone Administrator Principal · 1,690 posts #8

i just copied your code and compared with mine, mine showed that there were buddies online and the code of yours showed 0, ill take a look at it later again.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

GumSlone Administrator Principal · 1,690 posts #9

Yes it works, i just copied an pasted the code of you and forgot that i have changed the variable names for user_id in my script, but now it works..

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close