Mysql Online Buddy Count

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Mysql Online Buddy Count

    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:

    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.

    Code:
    $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


    #2
    post d online table structure, buddy table structure n the necessary part of your users table structure

    Comment


      #3
      Code:
      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

      Comment


        #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:

        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.

        Code:
        $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';
        [/b]
        I wonder that none gave you a fixed code anyway here is the fixed code that you wanted
        Code:
        $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"));

        Comment


          #5
          Thanks ill try it later
          Advertise your mobile site for FREE with AdTwirl

          Comment


            #6
            Thanks for trying to help but the code is not working
            Advertise your mobile site for FREE with AdTwirl

            Comment


              #7
              Thanks for trying to help but the code is not working[/b]
              No its working.I just tested it again and here are the data that I used to test user no 1's buddies count
              Code:
              -- 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
              Code:
              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
              Code:
              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

              Comment


                #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

                Comment


                  #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

                  Comment


                    #10
                    pleasure that I could help you

                    Comment

                    Working...
                    X