In message <fkrbnb+ug8p@xxxxxxxxxxx>, Ken <ken.poyser@xxxxxxxxxxx> writes >I found the solution!!! >and it works!!! > >I added the following code inside the while statement > > > $sql1 = "SELECT ORI from $table_name where ORI = '$ori'"; > $result1 =@mysql_query($sql1, $connection) or die >(mysql_error()); > $eagycount = @mysql_num_rows($result1); I think that you are correct, that there isn't a way to do it properly, i.e, all in one SQL command, because of the poor database design, containing all that repeated data. But I think that you will find "SELECT COUNT(ORI) FROM $table_name WHERE ORI = '$ori'" is going to be faster, but with the number of extra queries that you have had to introduce, there isn't going to be a quick method. -- Pete Clark Sunny Andalucia http://hotcosta.com/Andalucia.Spain