Q:

Duplicate ticket number in mysql

I want to find if there are any duplicate ticket number in my table.
my code below is displaying the first ticket number from classes table which is 1039, I want to diplay if there is a duplicate.  1039 is NOT a duplicate.  what is my problem?
$query = "SELECT ticket, COUNT(ticket) AS NumOccurrences FROM classes GROUP BY ticket HAVING (COUNT(ticket) > 1)";
$result = mysql_query($query) or die(mysql_error());    
while($row = mysql_fetch_array($result))    
{
$ticket = $row['ticket'];
}

if($result)
{
echo $ticket. "<br/>";
echo "there are " . mysql_num_rows($result) . " tickets ";
}

else
{
echo "no duplicate ";
}

A:

You need a GROUP BY clause, otherwise you're counting all tickets.
SELECT ticket, COUNT(*) c FROM classes GROUP BY ticket HAVING (c > 1)

