I have two tables that I have been struggling all day with doing a JOIN on. At this point i think I've pulled out just about all the hair I have left.
Unable to figure out the correct syntax for doing a JOIN, I somehow managed to do two queries and get (almost) the results I want.
The code I am using follows the questions at the bottom of this post
Question 1:
I have the results printing out, which is great, except for the fact that a few cells of table data have no content. Instead of just omitting the missing content, I get an empty line returned.
Here's the output:
COMPANY #1
Woodland Hills
CA
91367
USA
COMPANY #2
12345 Anystreet. St.
Anytown
CA
93720
USA
555-555-5555
555-555-5555
As you can see, there's a missing line betwixt the 1st company name and the city because there is no street address for this company in the db. Likewise, there are no phone or fax #s, so there's two extra lines that are empty before the next company name appears. Company #2 is actually displayed correctly as all fields are present in the db.
How can I just skip over these emtpy fields??
Question 2:
There HAS to be a better way of returning these results than what I've put together. I'm sure a JOIN would fit the bill, but I can't figger it out for the life of me.
I'm most concerned with having my results output correctly. If I can get help with the first question I can live with that... If Someone can help me with both questions, I will forever be in your debt.
Here's the code I am using:
/* Define first query */
$query = 'SELECT * FROM mailcode_ranges WHERE zip_low <= "'.$zipInput.'" AND zip_high >= "'.$zipInput.'"';
$result = mysql_query($query) or die("Query failed: " . mysql_error());
/* both queries & output */
while($row = mysql_fetch_array($result)) { // put results into an array
$ndc = $row["dist_id"];
/* Define second query */
$query2 = 'SELECT * FROM distributors WHERE "'.$ndc.'" = dist_id';
$result2 = mysql_query($query2) or die("Query failed: " . mysql_error());
while($row2 = mysql_fetch_array($result2)) { // put results into an array
$ndc2 = $row2["company"];
echo $row2[company] . "<br>";
echo $row2[address] . "<br>";
echo $row2[city] . "<br>";
echo $row2[state] . "<br>";
echo $row2[zip] . "<br>";
echo $row2[country] . "<br>";
echo $row2[phone] . "<br>";
echo $row2[fax] . "<br><br>";
}
}
Thank you, thank you, thank you, whoever you are!
-Chet23