I have a table named telco
which has 2 columns, id(PK)
and telco_prov
. I want to count the duplicate records in the telco_prov
column, then display the number of times it appeared.
telco
table example:
id telco_prov
1 Smart
2 Smart
3 Globe
4 Globe
5 Globe
Here is my code:
$query = "select telco_prov, count(*) c from telco group by telco_prov having c > 1";
$result = mysql_query($query) or die('Error: '.mysql_error ());
$row = mysql_fetch_assoc($result);
echo "SMART: <font color= 'red'>".$row['c']."</font>";
echo "<br>GLOBE: <font color= 'red'>".$row['c']."</font>";
The code has no error, but it doesn't do the counting correctly.
The result from the code above:
SMART: 2
GLOBE: 2
But it should be:
SMART: 2
GLOBE: 3
I've tried adding where
statement in the query but still it doesn't work.
$query = "select telco_prov, count(*) c from telco where telco_prov = 'Smart' group by telco_prov having c > 1";
Your help would be much appreciated. Thanks!
You have to loop the results. see the below code
$query = "select telco_prov, count(*) c from telco group by telco_prov having c > 1";
$result = mysql_query($query) or die('Error: '.mysql_error ());
while($row = $mysql_fetch_assoc($result)) {
echo $row['telco_prov'].": <font color= 'red'>".$row['c']."</font>";
}
Try with
while($row = mysql_fetch_assoc($result)){
echo strtoupper($row['telco_prov']).": <font color= 'red'>".$row['c']."</font>";
}
The code you write will give only the first row. For getting all the records, you need to loop though the record
the error is here
echo "SMART: <font color= 'red'>".$row['c']."</font>";
echo "<br>GLOBE: <font color= 'red'>".$row['c']."</font>";
this code will echo all telco_prov with their counts
while ($row = mysql_fetch_assoc($result)) {
echo '"'.$row['telco_prod'].'"':<font color="red">'.$row['c'].'</font>';
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.