简单的PHP MySQL检查行是否存在返回null结果[重复]

This question already has an answer here:

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

</div>

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.