...

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



ptmuldoon
01-17-2007, 11:42 PM
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.


$sql = 'SELECT COUNT(state) FROM game_'.$_SESSION['game_id']." WHERE state = 'dead' ";
$count_dead = get_one($sql);

GJay
01-17-2007, 11:54 PM
Did you try it?

It looks alright, but I'd question the sensible-ness of having a different table for each game...

koyama
01-17-2007, 11:55 PM
...could be right, but this depends on what get_one() is defined as.

Using built-in functions one would probably do


$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?
simultaneous post

ptmuldoon
01-18-2007, 02:49 PM
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;


// 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.

ptmuldoon
01-18-2007, 04:05 PM
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:



// 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(state) FROM 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++;
}

CFMaBiSmAd
01-18-2007, 04:27 PM
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) -

$sql = 'SELECT login FROM users;

guelphdad
01-18-2007, 04:54 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum