views:

107

answers:

2

I want to count the amount of times that a zip code is entered into a database. I'm not sure if I am using the right function or not. Also eventually I need to separate the zip codes by the year they were entered int the data base. I know how to separate the years-I think. What I really need help on is counting duplicate entries Here is my code.

$sql = 'SELECT * FROM zip ORDER BY time_register'; $result = mysql_query($sql,$db) or die(mysql_error(). "
SQL: $sql"); $row = mysql_fetch_array($result);

do{
  $visitor_zip= array();
  $register = $row['time_register'];
  $register_year = date(Y,$register);

 print_r(array_count_values($visitor_zip));

}while($row = mysql_fetch_array($result))

-----------------------------------------------------------------------------------


HERE IS MY FINAL CODE THAT WORKS!!!!!!!!

$sql = "SELECT visitor_zip AS zip, COUNT(visitor_zip) AS cnt FROM zip GROUP BY visitor_zip ORDER BY visitor_zip";

$result = mysql_query($sql,$db) or die(mysql_error(). "
SQL: $sql");

print '<table class="zip"><tr><td><b>Zip</b></td><td># of</td></tr>';
while($row = mysql_fetch_array($result))
{
  print '<tr><td>' . $row['zip'] .'</td><td>'.  $row['cnt'] . '</td></tr>';
}
print '</table>';
A: 

I think this should work:

SELECT COUNT(*) AS cnt, zipcode FROM zip
GROUP BY zipcode
ORDER BY time_register
Steve Wortham
You probably want to add a min(time_register) in there somewhere, assuming that the FIRST entered date for each zip code is the one of interest.
Carl Manaster
I appreciate all the help that I have gotten already. I was completely on the wrong track trying to solve this problem. I was trying to find a php function to do this instead of changing the query to the database. Now I don't know how the write the code to display the information from the data base. Can you help!?
I haven't touched php in 5 years so I'm probably not the one to ask. You might want to add this to your original question.
Steve Wortham
A: 
select count(ZIPCODEFIELD) as cnt, ZIPCODEFIELD, time_register FROM zip
GROUP BY zipcode,time_register
Sk93