如何计算条件为

时间:2015-09-08 19:11:43

标签: php mysql

所以我试图让查询计算该位置的玩家总数,如果他们在同一个位置,并在线。但我当前的查询返回0.不知道该尝试什么,已经卡住了几天。

<?php 
    include('connect.php'); 

            $town = $info['location'];

            $grabTown = mysql_query("SELECT * FROM `location` WHERE `name` = '$town'");
            $t = mysql_fetch_array($grabTown);

            $totPlayers = mysql_query("SELECT COUNT(*) AS player_total FROM `user_info` WHERE `location` = '$town' AND 'online' = '1'");
            $totalPlayers = mysql_fetch_array($totPlayers);
            $playersTotal = $totalPlayers['player_total'];
?>

2 个答案:

答案 0 :(得分:0)

这是你的问题:

$totPlayers = mysql_query("SELECT COUNT(*) AS player_total FROM `user_info` WHERE `location` = '$town' AND `online` = '1'");

你有&#34;在线&#34;用引号代替反引号。

答案 1 :(得分:0)

请记住,PHP的mysql_ *函数正在消亡,切换到mysqli或pdo之类的东西。在我提交的代码中查看我的评论。 告诉我它是否有效。欢呼声。

include('connect.php'); 

$town = $info['location'];

$grabTown = mysql_query("SELECT * FROM `location` WHERE `name` = '$town'");

// Added error check
if (!$grabTown) {
    die('Invalid query: ' . mysql_error());
}

$t = mysql_fetch_array($grabTown);

// Changed '' to `` on { 'online' } and removed '' from { '1' }
// Don't know if removing '' on { '1' } is required. I think it's fine either way.
$totPlayers = mysql_query("SELECT COUNT(*) AS player_total FROM `user_info` WHERE `location` = '$town' AND `online` = 1");

// Added error check
if (!$totPlayers) {
    die('Invalid query: ' . mysql_error());
}

$totalPlayers = mysql_fetch_array($totPlayers);
$playersTotal = $totalPlayers['player_total'];