Jump to content

INNER JOIN


timmah1

Recommended Posts

Why do I keep getting an error with this??

<?php
include('inc/connect.php');
$query = "SELECT mrk.state, mrk.company, mrk.address1, mrk.postal, mrk.city, mrk.latitude, mrk.longitude, zipdata.zip
FROM mrk 
INNER JOIN zipdata
ON mrk.postal=zipdata.zip";
$result = mysql_query ($query);
while ($row = mysql_fetch_assoc ($result)) {

echo $row['company'];
}
?>

Link to comment
https://forums.phpfreaks.com/topic/259172-inner-join/
Share on other sites

  Quote

Is mrk and  and zipdata real name of your table ?

 

Yes, that's the name of my tables. The error I'm getting is this

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/redneck/public_html/test/test1.php on line 70

 

Now this is for a store locator, this is the entire script

<?php

// Create page variables
$r = NULL;
$z = NULL;
$stores = NULL;
$Errors = NULL;

    include('inc/connect.php');

// Declare page functions
function Dist ($lat1, $lon1, $lat2, $lon2) { 

  $distance = sin(deg2rad($lat1))
      * sin(deg2rad($lat2))
      + cos(deg2rad($lat1))
      * cos(deg2rad($lat2))
      * cos(deg2rad($lon1 - $lon2));

  $distance = (rad2deg(acos($distance))) * 69.09;
  return $distance;

}

### Handle form if submitted
if (isset ($_POST['submitted'])) {

  // Validate Postcode code field
  if (!empty ($_POST['zip']) && is_numeric ($_POST['zip'])) {
  
    $z = (int)$_POST['zip'];

// Verify Postcode code exists
    $query = "SELECT latitude, longitude FROM zipdata WHERE zip = '$z'";
    //$result = mysql_query ($query);
    $result=mysql_query($query) or die ('Invalid query.');


    if (mysql_num_rows ($result) == 1) {
      $zip = mysql_fetch_assoc ($result);
    } else {
      $Errors = '<p>The postcode code you entered was not found!</p>';
  $z=NULL;
    }

  }

  // Validate radius field
  if (isset ($_POST['radius']) && is_numeric ($_POST['radius'])) {
    $r = (int)$_POST['radius'];
  }

  // Proceed if no errors were found
  if ($r && $z) {

    // Retrieve coordinates of the locations
$query = "SELECT state, company, address1, postal, city, latitude, longitude
	FROM mrk 
	INNER JOIN zipdata
	ON mrk.postal=zipdata.zip";

    $result = mysql_query ($query);

    // Go through and check all locations
    while ($row = mysql_fetch_assoc ($result)) {


      // Separate closest locations
      $distance = Dist ($row['latitude'], $row['longitude'], $zip['latitude'], $zip['longitude']);

      // Check if store is in radius
      if ($distance <= $r) {

        $stores[] = array (
          'name'      => $row['company'],
          'address'   => $row['address1'],
          'state'     => $row['state'],
          'town'      => $row['city'],
          'postal'    => $row['postal'],
        );

      }

    }

  } else {
    $Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>';
  }

}

?><html>
<head>
<title>Store Locator</title>
</head>
<body>
<form action="" method="post">
  <p>Enter your zip code below to find locations near you.</p>
  <?php echo ($Errors) ? $Errors : ''; ?>
  <div>
    <label>Zip:</label>
    <input name="zip" type="text" size="10" maxlength="5" />
  </div>
  <div>
    <label>Search Area:</label>
    <select name="radius" id="radius">
      <option value="5">5 mi.</option>
      <option value="10">10 mi.</option>
      <option value="15">15 mi.</option>
      <option value="20">20 mi.</option>
      <option value="50">50 mi.</option>
      <option value="100">100 mi.</option>
    </select>
  </div>
  <div>
    <input type="hidden" name="submitted" value="submitted" />
    <input type="submit" value="Submit" />
  </div>
</form>

<?php

if (isset ($stores)) {

  if (!empty ($stores)) {

    echo '<p><strong>' . count ($stores) . ' results were found.</strong></p>';
    foreach ($stores as $value) {

      echo '<p><strong>' . $value['name'] . '</strong><br />';
      echo $value['address1'] . '<br />';
      echo $value['town'] . ', ' . $value['state'] . ' ' . $value['postal'];
      $value['address'], ' ',
      $value['town'], ', ',
      $value['state'], ' ',
      $value['postal'],
      '">Map this location</a><br />';
      echo '</p>';

    }

  } else {
    echo '<p><strong>No results found</strong></p>';
  }

}

?>
</body>
</html>

 

Link to comment
https://forums.phpfreaks.com/topic/259172-inner-join/#findComment-1328885
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.