I’m working in MySQL/PHP. It’s a zip code database. The first table has the zip codes and the average AGI for each zip code. But this is not a complete table of zip codes, because the IRS doesn’t include zip code data that has less than 250 tax returns filed. Also, some zip codes are for PO Boxes and there might not be any average AGI data for those zip codes either.
I want to be able to search the first table which has the average AGI by the zip code, and if the zip code is not found there I want it to search the second table, and display a message something this in PHP:
echo "The $zipcode was not found." . "<br>";
if (isset($pobox)) {
echo "Because $zipcode is a PO Box" . "<Br>";
} else {
echo "It may contain fewer than 250 tax returns filed." . "<br>";
}
Here is the query for the MySQL in PHP:
$result = mysql_query("
select zip
from zip_codes
where zip=$zipcode;") or exit(mysql_error());
The second TABLE, is called zip_code_types which contains if the zip code is for a PO Box, or something else unique about it.
Should this be done by combining the two tables in a single search? Or should two separate queries be done?
How do I check if the first MySQL query doesn’t find anything? What is returned and where, is there another function that’s needed to check for this? If I’m completely missing how this should be done, please kindly enlighten me. Thanks!
Use of mysql_ functions is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.
To answer your question, you could check for number of rows returned by your SELECT query and proceed to next query if needed, like:
Do you mean something like this