Reputation: 39
So I have a count for a database and I'm currently struggling. It tells me there's 1 result no matter how many are there. I am trying to get the code to count the number of teams with the league set to 5, and there are definitely more than one, but it continuously, the code I have written, returns the number 1. Here is the code:
function inactive_team_search() {
$count = mysql_query("SELECT * FROM `teams` where `league`=5");
echo count($count);
}
Any help is much appreciated! Probably a silly error where I've been coding for too long but just can't figure it out!
Upvotes: 0
Views: 58
Reputation: 16297
function inactive_team_search() {
$result = mysql_query("SELECT * FROM `teams` where `league`=5");
echo mysql_num_rows($result);
}
Upvotes: 0
Reputation: 10084
Try this as your query instead; this will actually return the count of teams with a league of 5.
SELECT COUNT(*) FROM `teams` WHERE `league`=5
In addition, mysql_query returns its own data structure, which you need to interpret.
function inactive_team_search() {
$query = "SELECT COUNT(*) inactiveCount FROM `teams` WHERE `league`=5";
$result = mysql_query($query);
if(!$result) {
echo 'Could not run query.';
}
else {
$row = mysql_fetch_array($result);
echo $row['inactiveCount'];
}
}
Finally, as an aside, mysql_query is deprecated starting in PHP 5.5, so it'd be good for you to switch to using either MySQLi or PDO.
Upvotes: 1
Reputation: 62
Use this code:
function inactive_team_search()
{
$result = mysql_query("SELECT COUNT FROM `teams` where `league`=5");
$count = mysql_fetch_array($result);
echo $count[0];
}
Upvotes: 1