query and then query again

I need to take the results from a query and  explode it and then use each item in the array in a new query.

 my code is wrong but maybe this will help explain


$myarray = explode(',', $zipcodes);
       foreach($myarray as $value) {
     //   echo "$value<br>\n";
       }
 
 
$sql4 ="SELECT d.zip  FROM db_publication_report d 
  WHERE d.`zip` LIKE '%$value%' AND d.`id_publisher` = '$Publisher_id'";
$result4 = mysql_query($sql4) or die(mysql_error());
echo"ZIPCODE:$sql4 <br><br>";
 
if(mysql_num_rows($result) > 0) 
{
  while($row = mysql_fetch_assoc($result4)) 
  {
    $zip = $row['zip'];
 
    echo "&nbsp;&nbsp;<b>Zip Code:</b>$zip<br>&nbsp;&nbsp;";
  }
} else {
  // Print an error if no user is found.
  echo "No zip was found in that zipcode";
}

Open in new window

MasConAsked:
Who is Participating?
 
MasConAuthor Commented:
that didnt work to well becasue both the zip fields in the tables have comma separated values.

here is my rewrite and what I am trying to do is:

1st query gets the publisher id and zipcodes they have license to that matched a form submission.
(there may be more than one publisher)

2nd query gets there name and address

3rd query takes the publisher ID and looks up the zipcodes they published into


from there I want to take the 2 arrays (one from the 1st query called $Owned_Zip and one from the 3rd Query called $Pub_Zip) and compare them to the zipcode entered. if the zip code is on bot lists it prints out OK if they are not in both arrays then it prints out their is a license violation.
$zipcode = ($_POST['zip']);
 
echo "<br>Here are the results for zip code: <div class=seek>$zipcode</div><br>
  <br><font color=#0072bc size= 3><b><u>ZIP CODE RESULTS</u></b></font><br><br>";
$Publisher_id= '';
$Owned_Zip= '';
$Pub_Zip = '';
 
//Get the Pulishers ID , Zip Codes they own
//echo" All Publisher ID's with that Zip Code:<br>";
$dbname = 'adnet';
mysql_select_db($dbname);
$sql = "SELECT p.`pdata_id_publisher`, p.`pdata_zipcodes` 
  FROM publisher_data p where `pdata_zipcodes` LIKE '%$zipcode%';";
//	echo"$sql";
$result = mysql_query($sql) or die("Couldn't execute query");
 
while($row = mysql_fetch_assoc($result)) 
{
	$Publisher_id =  $row['pdata_id_publisher'];
  	$Owned_Zip =  $row['pdata_zipcodes'];
  
//  echo"$Publisher_id <br>";
 
//Get Publishers Name, Phone and Job Number
  $dbname = 'adnet';
  mysql_select_db($dbname);
$sql2 = "SELECT d.`jobno`, d.`contact`, d.`phone` FROM db_publishers d  
    WHERE d.`id_publisher` = '$Publisher_id'";
//	echo"$sql2";
$result2  = mysql_query($sql2) or die(mysql_error());
 
while($row = mysql_fetch_assoc($result2)) 
{
	$contact=$row['contact'];
    $phone=$row['phone'];
    $jobno=$row['jobno'];
 
 echo "<br><font color=#0072bc size= 3><b><u>License Info:</u></b></font><br><BR>&nbsp;&nbsp;<b>Publisher:&nbsp;</b>$contact<br>&nbsp;&nbsp;<b>Zip Codes Owned:&nbsp;</b>$Owned_Zip<br>&nbsp;&nbsp;<b>Phone:&nbsp;</b>$phone<br>&nbsp;&nbsp;<b>Job No.:&nbsp;</b>$jobno<br><br>";
// print_r (explode(" ",$Owned_Zip));
 
 
  $dbname = 'adnet2010';
  mysql_select_db($dbname);
$sql3 = "SELECT d.community,d.code,d.zip, d.inactive_edition  FROM db_publication_report d 
  WHERE d.`id_publisher` = '$Publisher_id'";  
//echo"$sql3<br>";
  
$result3  = mysql_query($sql3) or die(mysql_error());
while($row = mysql_fetch_assoc($result3)) 
{
    $community=$row['community'];
	$Pub_Zip=$row['zip'];
    $active=$row['inactive_edition'];
	$code=$row['code'];
		
//echo "<br><font color=#0072bc size= 3><b><u>Publishing Info:</u></b></font><br><br>&nbsp;&nbsp;<br>&nbsp;&nbsp;<b>Published Zip Codes:</b>$Pub_Zip&nbsp;";
 
//	echo"OWNED:$Owned_Zip<BR><BR>";
//echo"Published Zips:$Pub_Zip";
 
 
 
		}		
	}
}
//echo"Searched for:<b>$zipcode</b>";
//echo"<br>in the Array of:<b>$Owned_Zip</b>";
 
$Owned = array('$Owned_Zip');
 
if (in_array("$zipcode", $Owned, true)) {
    echo "The Zip Code is Owned\n";
}
 
else {
echo"There is a violation for $zipcode";
}

Open in new window

0
 
Lukasz ChmielewskiCommented:
try
$myarray = explode(',', $zipcodes);
       foreach($myarray as $value) {
     //   echo "$value<br>\n";
 
 
$sql4 ="SELECT d.zip  FROM db_publication_report d 
  WHERE d.`zip` LIKE '%$value%' AND d.`id_publisher` = '$Publisher_id'";
$result4 = mysql_query($sql4) or die(mysql_error());
echo"ZIPCODE:$sql4 <br><br>";
 
if(mysql_num_rows($result) > 0) 
{
  while($row = mysql_fetch_assoc($result4)) 
  {
    $zip = $row['zip'];
 
    echo "  <b>Zip Code:</b>$zip<br>  ";
  }
} else {
  // Print an error if no user is found.
  echo "No zip was found in that zipcode";
}
 
 
 
       }
 
 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.