PHP Help Needed!

Discussion in 'PHP' started by zapshot, Dec 29, 2009.

  1. #1
    Hi,
    i need help for following issues:

    1 How to display total number of members from by database in php file..
    please post a php code

    2 How to display total number of male and female members from my database.
    please post a php code

    i have a database where std_user is username std_gender is for gender
    thanx
     
    zapshot, Dec 29, 2009 IP
  2. kleinnico

    kleinnico Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I didn't try, but it would become something like this:

    $query_total = mysql_query("SELECT count(1) FROM users") or die(mysql_error());
    $total = mysql_result($query_total,0);
    
    $query_male = mysql_query("SELECT count(1) FROM users WHERE std_gender = 'male'") or die(mysql_error());
    $male = mysql_result($query_total,0);
    
    $female = $total - $male;
    
    echo "Total: ". $total ."<br />
    Male: ". $male ."<br />
    Female: ". $female ."";
    PHP:
    Good luck.
     
    kleinnico, Dec 29, 2009 IP
  3. zapshot

    zapshot Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanx
    worked fine with little change
     
    zapshot, Dec 30, 2009 IP
  4. jonno_81

    jonno_81 Well-Known Member

    Messages:
    81
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    #4
    This code works technically, but I'd like to just make a small note:

    $female = $total - $male;

    The above line of code counts the total number of females, however, I would prefer to actually get the count from the database:

    $query_female = mysql_query("SELECT count(1) FROM users WHERE std_gender = 'female'") or die(mysql_error());
    $female = mysql_result($query_female,0);


    The reason I say this is because if there are some records in the database where the gender have not been assigned then using the arithmetic above will give you a higher female count than what actually exists.

    You should probably then also include:

    $query_unknown = mysql_query("SELECT count(1) FROM users WHERE std_gender not in ('male', 'female')") or die(mysql_error());
    $unknown = mysql_result($query_unknown,0);


    Just a personal preference...

    PS, kleinnico, where are you from? Your name means little nico in Afrikaans?
     
    jonno_81, Dec 30, 2009 IP
  5. kleinnico

    kleinnico Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you indeed don't force the user to enter the right gender then you might get that problem yep.


    Offtopic:
    Hhehe I'm from the Netherlands. In Dutch it also means little nico :p (as Dutch and Afrikaans looks a little bit similar) Actually I've been always pretty big and my last name is also big in Dutch. So thats why I've chosen that nickname some years ago ;) hehe :p
     
    kleinnico, Dec 30, 2009 IP