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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.