Do a simple debugging. Check if your query is OK. Insert this code after line 19:
die($checksql);
and enter the displayed query in phpMyAdmin (or other client). It should return 0 records or 1 record.
Or check what is the count that the query returns. Insert this code after line 21:
die($count);
It should display 0 or 1.