Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Dormilich

Thread: User count in database

  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts

    User count in database

    So I am using mysqli queries and finding it impossible to get the number of users in my database. I thought this code would work but it doesn't.

    PHP Code:
     $sQry mysqli_query($CharNameDB"SELECT count(*) FROM rod_AllUsers");
    $obQry mysql_query($sQry);
    $count 0;
    if (
    false !== $obQry && mysql_num_rows($obQry) == 1)
    {
        
    $record mysql_fetch_row($obQry);
        
    $count $record[0];


    The error I get is:

    Warning: mysql_query() expects parameter 1 to be string, object given in /home1/newrodga/public_html/index.php on line 8 Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /home1/newrodga/public_html/index.php on line 10
    Thanks for any help
    Last edited by myfayt; 05-11-2014 at 06:03 PM.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Is each row in your table a unique user?

    You can then find out the number of rows in the table?

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,335
    Thanks
    13
    Thanked 348 Times in 344 Posts
    you simply cannot mix mysqli functions with mysql functions.
    Strider64 likes this.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    This doesn't work either.

    PHP Code:
     $sQry mysqli_query($CharNameDB"SELECT count(*) FROM rod_AllUsers");
    $obQry mysqli_query($sQry);
    $count 0;
    if (
    false !== $obQry && mysqli_num_rows($obQry) == 1)
    {
        
    $record mysqli_fetch_row($obQry);
        
    $count $record[0];


    Warning: mysqli_query() expects at least 2 parameters, 1 given in /home1/newrodga/public_html/index.php on line 8 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home1/newrodga/public_html/index.php on line 10
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Ok so this one works for registered users.

    PHP Code:
    if ($result mysqli_query($CharNameDB"SELECT id FROM rod_AllUsers ORDER BY id")) {

        
    /* determine number of rows result set */
        
    $row_cnt mysqli_num_rows($result);

        
    /* close result set */
        
    mysqli_free_result($result);
        
    mysqli_close($CharNameDB);
    }
                            echo 
    "<li class=\"first\"><b>Registered Players:</b> $row_cnt </li>"
    However for my recently online players, this doesn't work below.

    PHP Code:
    $onlinequery mysqli_query($CharNameDB"SELECT * FROM rod_AllUsers WHERE UNIX_TIMESTAMP(onlinetime) >= '" . (time() - 600) . "' ORDER BY id");
                                                
    $onlinenum mysqli_num_rows($onlinequery);
                            echo 
    "<li><b>Players Online:</b> $onlinenum</li>"

    Warning: mysqli_query(): Couldn't fetch mysqli in /home1/newrodga/public_html/index.php on line 128 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home1/newrodga/public_html/index.php on line 129
    Thanks
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #6
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    If those snippets are in the same file
    PHP Code:
    mysqli_close($CharNameDB); 
    has closed the database connection.

  • Users who have thanked schleppel for this post:

    myfayt (05-11-2014)

  • #7
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Whoops! wrong placements! Thank you
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,338
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by myfayt View Post
    This doesn't work either.

    PHP Code:
     $sQry mysqli_query($CharNameDB"SELECT count(*) FROM rod_AllUsers");
    $obQry mysqli_query($sQry); 
    No it won't work. Do you not see what you have done?

    You've set the result of a query (which will be an object) and then tried to run another query using that object which will of course result in a mysql error which is then assigned to the a variable and then used in your num_rows() call.

    Delete the second mysqli_query() call (the one without the connection specified).
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

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