Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: PHP Question

  1. #1
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default PHP Question

    Haven't done anything in PHP in around 6 months so I don't remember.

    How do you count the number of users in a table again?

    Also, how do I see what users are online? My system uses sessions so I guess I would set a cookie?

    Sorry for the simple questions, but again, I haven't done anything in around 6 months.
    Vouches
    [x][x]

  2. #2
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default

    Also, I'm getting:


    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/fazon/public_html/test/config.php on line 42

    It's coming from:

    // User's info
    $query = mysql_query("SELECT * FROM users WHERE username = '{$_SESSION['username']}'");
    $users = mysql_fetch_array($query);
    Vouches
    [x][x]

  3. #3
    Join Date
    Apr 2009
    Location
    Illinois, USA
    Posts
    130
    Tokens
    359

    Default

    To count the number of users you have you use mysql_num_rows()

    Example:
    PHP Code:
    <?php
    include("config.php"); //where MySQL connection details are

    $query mysql_query("SELECT * FROM users"); //gets info from users
    $count mysql_num_rows($query); //counts the amount of users

    echo $count//echos the amount of users
    ?>
    I don't know anything about PHP and sessions.


    My last reputation was from ThisNameWillDo!.

  4. #4
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    7,554
    Tokens
    0

    Latest Awards:


  5. #5
    Join Date
    Oct 2007
    Posts
    824
    Tokens
    71

    Latest Awards:

    Default

    Thanks guys, what about the error I get?
    Vouches
    [x][x]

  6. #6
    Join Date
    Apr 2009
    Location
    Illinois, USA
    Posts
    130
    Tokens
    359

    Default

    Well, I don't know how to do it via sessions, but I could show you another way I do it. It may not be the best way of doing it, but it gets the job done.

    First, I added a column to my "database", or whatever it's called to users called last_active. Every time a user is logged in, it gives them a time stamp and adds it to last_active (every page they load while logged in it updates the time stamp).
    PHP Code:
    //last active
    if($logged) {
          
    $time date("omdHis");
          
    $updatetime mysql_query("UPDATE users SET `last_active` = '$time' WHERE `username` = '$logged[username]'");
    }else{


    Then I use the time stamp with a simple function that I created.
    PHP Code:
    function whosonline() {
        
    //current time
        
    $timestamp date("omdHis");

        
    //subtracts 15 minutes of the current time
        
    $subtract = (date('omdHis'strtotime('- 15 minutes')));

        
    $result mysql_query("SELECT * FROM users WHERE `last_active` > '$subtract' ORDER BY username DESC");
        if(
    mysql_num_rows($result)) {
            echo 
    "There's currently no user online.";
        }else{
            
    //loops until everyone has been added
            
    while($online mysql_fetch_array($result)) {

                
    //the users information
                
    $onlineuser $online['username'];

                echo 
    '<a href="profile/'.$onlineuser.'">'.$onlineuser.'</a> ';
            }
        }

    As I said above, it may not be the best, but it gives a rough idea of how you could do it.


    My last reputation was from ThisNameWillDo!.

  7. #7
    Join Date
    Apr 2009
    Location
    Illinois, USA
    Posts
    130
    Tokens
    359

    Default

    Ugh sorry for the double post (but we can't edit our previous posts which is lame), but I think that your getting an error because of the 's wrapped around username in the SQL query.

    It should look like the following:
    $_SESSION[username]
    Last edited by RastaLulz; 31-12-2009 at 06:01 PM.


    My last reputation was from ThisNameWillDo!.

  8. #8
    Join Date
    Sep 2009
    Location
    Hull
    Posts
    827
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by Foskett View Post
    Ugh sorry for the double post (but we can't edit our previous posts which is lame), but I think that your getting an error because of the 's wrapped around username in the SQL query.

    It should look like the following:
    $_SESSION[username]
    Thats surely doesn't matter? I use $_SESSION["name"]; & $_SESSION['name']; all the time without errors...

    Lew.
    Im not here to be loved, I love to be hated :-}


  9. #9
    Join Date
    Apr 2009
    Location
    Illinois, USA
    Posts
    130
    Tokens
    359

    Default

    Quote Originally Posted by Lewiie15 View Post
    Thats surely doesn't matter? I use $_SESSION["name"]; & $_SESSION['name']; all the time without errors...

    Lew.
    It surely gives me errors when I do it because it's cutting off the string (or whatever) when you do that.
    Last edited by RastaLulz; 01-01-2010 at 05:26 PM.


    My last reputation was from ThisNameWillDo!.

  10. #10
    Join Date
    Apr 2009
    Location
    Illinois, USA
    Posts
    130
    Tokens
    359

    Default

    Sigh, something really needs to be done about editing posts.. :eusa_pray

    But now that I think of it, it most likely isn't that because it'd return:
    Code:
    Parse error:  parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\xampp\htdocs\yoursite\file.php on line line
    So, it's most likely that $_SESSION['username']; doesn't mean anything, because there's no argument for MySQL, but then again I could be wrong. But mysql_fetch_array has nothing to get, therefore it's putting out an error.


    My last reputation was from ThisNameWillDo!.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •