Reputation: 393
I have a database which contains fisheries in different regions. I am using the command below:
$sql ="SELECT region FROM table WHERE region='Fife'";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)){
$total = array ($sql);
echo count($total);
This gives me (111111111111111) as an output. However i am looking for it to return (15) instead. There are currently only 15 enteries which match 'Fife'
Can anyone suggest how i can improve my code to get the desired results??
Upvotes: 1
Views: 603
Reputation: 219804
Use MySQL's count()
:
$sql ="SELECT count(*) AS total FROM table WHERE region='Fife'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($res);
echo $row['total'];
Upvotes: 4
Reputation: 8448
You can actually do the count in SQL, which is a lot faster.
$sql ="SELECT COUNT (*) FROM table WHERE region='Fife'";
$c = mysql_query($sql) or die(mysql_error());
print $c;
Upvotes: 0
Reputation: 910
$sql ="SELECT region FROM table WHERE region='Fife'";
$res = mysql_query($sql) or die(mysql_error());
$total = array();
while($row = mysql_fetch_assoc($res)){
$total[] = array ($sql);
}
echo count($total);
If you want to keep the records
Upvotes: 0