0

I'm trying to build a simple postcode/ zip code check for our restaurant to check if a customer is within a list of postcodes we serve. Every time I query the postcode it returns a "not found" result and I can't work out why.

 $result = mysql_query("SELECT * FROM functions-postcodes WHERE postcode = 'PO111XX'");
if(mysql_num_rows($result) == 0) {
     echo "Postcode not within coverage area (postcode not found in table)";
} else {
     echo "Postcode IS within coverage area";
}

Here is what the database table looks like: Database screenshot

Rick
  • 9
  • 1
  • 5
  • Have you check these same query execute in phpmyadmin is it gives error? – Dhaval Bhavsar Mar 05 '17 at 11:59
  • is it 0 or O ? It looks like You have a big letter O stored in database. – Whencesoever Mar 05 '17 at 11:59
  • If you're writing new code, **_please_ don't use the `mysql_*` functions**. They are old and broken, were deprecated in PHP 5.5 (which is so old it no longer even receives security updates), and completely removed in PHP 7. Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Mar 05 '17 at 12:07

1 Answers1

1

Your table name should not have a - in it. Consider renaming the table to functions_postcode or something like that. If that is not possible, use backticks to escape special characters in (table) names:

SELECT * FROM `functions-postcodes` ...

That, and:

  1. Check for errors when you do not get the desired result (in logs or with error function calls)
  2. Stop using mysql_*. They are deprecated. Use MySQLi or PDO instead. Use prepared statements too.
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195