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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    my count does not seem to be working with this variable [was: Is this right?]

    I'm still a rookie at this stuff. But is this sql query right?

    I'm trying to count the number of players in a 'dead' state from a game.

    PHP Code:
    $sql 'SELECT COUNT(state) FROM game_'.$_SESSION['game_id']." WHERE state = 'dead' ";
            
    $count_dead get_one($sql); 
    Last edited by guelphdad; 01-18-2007 at 04:52 PM. Reason: provide proper thread title

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Did you try it?

    It looks alright, but I'd question the sensible-ness of having a different table for each game...
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    ...could be right, but this depends on what get_one() is defined as.

    Using built-in functions one would probably do
    PHP Code:
    $sql 'SELECT COUNT(state) FROM game_'.$_SESSION['game_id']." WHERE state = 'dead' ";
    $result mysql_query($sql);
    list(
    $count_dead) = mysql_fetch_row($result); 
    Are you getting error messages?
    Edit: simultaneous post
    Last edited by koyama; 01-18-2007 at 12:00 AM.

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Thanks guys. I got the above figured out. Turns out the error wasn't pointing to that line afterall.

    But I'm to one more step here: FYI, each game does require its own name.

    Essentially, I'm looking to count up the number of players in the games_xx table, and that number to the user's total_players field. I've learned alot in the last few days, and think I need foreach command, but not sure how to go about it. This is what I have so far, and I hope its right;

    PHP Code:
    // Add total players in game to user table
    $sql 'SELECT player FROM game_'.$_SESSION['game_id'];
    $ply_name get_one($sql);

    $sql 'SELECT COUNT(player) FROM game_'.$_SESSION['game_id'];
    $players get_one($sql);
    $numplayers "$players) - 1"
        
    $sql 'SELECT total_players FROM users WHERE login = \''.$ply_name.'\'';
    $total_plyr get_one($sql);
    $total_player "$total_plyr + $numplayers"
    I think they above looks correct, but I'm not sure on the last step on how to search the users table and UPDATE the total_players field to the new $total_player string.

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Well, I'm trying here:

    I put this together, and still unsure if I'm closer or not, as I'm getting an T_STRING error:

    PHP Code:
    // Add total players in game to user table
    // This is not working yet.  Need to research it
    $sql 'SELECT player FROM game_'.$_SESSION['game_id'];
    $ply_name get_one($sql);

    $sql 'SELECT login FROM users;
    $login = get_one($sql);

    $sql = '
    SELECT COUNT(stateFROM game_'.$_SESSION['game_id']." WHERE state = 'waiting' "; // This is the line giveing a T_String Error
    $numplayers = get_one($sql); 

    $i=1;
    foreach($ply_name == $login)
        $sql = '
    SELECT total_players FROM users WHERE login ''.$ply_name.'\'';
        
    $total_plyr get_one($sql);
        
    $total_player "$total_plyr + $numplayers";
        
        
    $sql 'UPDATE users SET total_players = '.$total_player.' WHERE login = \''.$ply_name.'\'';
        
    $result single_qry($sql);
        
    $i++;


  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,132
    Thanks
    2
    Thanked 328 Times in 320 Posts
    Sorry to jump in here (and to be blunt).

    The first step in learning any language, a linguistic language or a programming language, is to learn the syntax rules for that language.

    The first step in debugging a syntax error, is to proof read your code. The following line is missing something (exactly what it is missing I will leave to you as a learning exercise) -
    PHP Code:
    $sql SELECT login FROM users
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    additional help would be to include the actual warning message in FULL and not just in part. along with that you would provide the line in question plus a couple of lines above and below the line the error reports on.

    Don't post your entire code.

    As for your table layout, no you don't need a separate table layout for each game. Have a read on database normalization and you will see that you can probably come up with a better layout for your tables.


  •  

    Posting Permissions

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