So I'm trying to have the query count the total number of players at the location if they're at the same location, and online. But my current query is returning 0. Not sure what to try, been stuck for a few days.
<?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'];
?>
Here's your problem:
$totPlayers = mysql_query("SELECT COUNT(*) AS player_total FROM `user_info` WHERE `location` = '$town' AND `online` = '1'");
You had "online" in quotes instead of backticks.
Remember that PHP's mysql_* functions is dying, switch to something like mysqli or pdo or whatever. Check my comments in the code I submitted. Tell me if it works. Cheers.
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'];