0

I have a mysql database that collects application forms. Each application form gets copied into the DB.

Example :

ID  l  Username      
01     MW001    
02     MW002    
03     MW001    
04     MW001

I want to get the value of the duplicates in a number by using a session as they login to there account a session gets created where i currently show them all there details , example

MW001 has 3 applications If i could get the full query and how to display it, this is what i used.

$sql = "SELECT affID, COUNT(*) FROM taffiliate WHERE affID = '" . $_SESSION['affID'] . "'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo  $row['affID'] ; 

In short i just want to show them they applied 3 times.

LHristov
  • 1,103
  • 7
  • 16
user3485335
  • 45
  • 1
  • 9
  • add a group by username – John Ruddell Aug 14 '14 at 14:43
  • Ok So this is the query i tried and it is now working $sql = "SELECT affID, COUNT(*) as c FROM taffiliate WHERE affID = '" . $_SESSION['affID'] . "'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); echo $row['c'] ; – user3485335 Aug 14 '14 at 15:12
  • $sql = "SELECT affID, COUNT(*) as c FROM taffiliate WHERE affID = '" . $_SESSION['affID'] . "'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); echo $row['c'] ; – user3485335 Aug 14 '14 at 15:12

1 Answers1

0

Your query is fine, you just need to give an alias to that COUNT(*)

SELECT affID, COUNT(*) as c FROM taffiliate...

Then you can get that value with

echo $row['c'];

And since you already have the affID and your WHERE clause is restricted to only that, you don't need MySQL to return the affID. Your query can even be

 SELECT COUNT(*) as c FROM taffiliate WHERE affID=123    // 

Also, you can execute your query in many better ways after reading this

How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95