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 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    99
    Thanks
    16
    Thanked 0 Times in 0 Posts

    SELECT COUNT help

    Hi, I have a function that counts how many users are online, and displays them to the user.

    Now I want to display how many a credits a user has remaining.

    The first function for how many users works fine, but the second function to count how many credits a user has does not. I think I got the logic to the second function completely wrong.

    Code:
    <?php
    function users_online() {
    	return mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `logged_in` = 1"), 0);
    }
    
    function credits() {
    	return mysql_result(mysql_query("SELECT COUNT(`credits`) FROM `users` WHERE `user_id` = $user_id"), 0);
    }
    ?>
    Could someone explain where I have gone wrong please?

    Thanks

    Paul

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,589
    Thanks
    0
    Thanked 644 Times in 634 Posts
    You left out the quotes in = '$user_id'" - all non-numeric values need to be entered as text strings.

    You should also be looking at replacing the obsolete mysql_ calls with either mysqli_ or PDO as mysql_ will be removed in the next version of PHP.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Almost surely you have that wrong. If each users record contains a field named credits and that field is the total credits belonging to that user, then you do *NOT* need COUNT, at all. Just:
    Code:
    function credits() {
    	return mysql_result(mysql_query("SELECT credits) FROM users WHERE user_id = $user_id"), 0);
    }
    And Felgall is right about the use of apostrophes around $user_id, but I personally would bet that the DB field user_id *IS* a number and so you do NOT want the apostrophes.

    But if you want help with SQL questions, you *REALLY REALLY REALLY* need to post the SCHEMA of your db tables and, usually, a few sample records from that table.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Select Count is best query of mysql to count the specific array's element..


  •  

    Posting Permissions

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