Jump to content

Help selecting distinct values from 3 columns


brooksh

Recommended Posts

I am trying to create an autocomplete form for 'city,state zip'  I want to be able to search by either a distinct zip code that will show 'city, state zip' or by distinct city 'city, state'

 

Can anyone tell me how to fix my script?

 

$sql = "SELECT DISTINCT zip,city,state FROM `residential` WHERE `zip` LIKE '$input%' OR `city` LIKE '$input%' OR `state` LIKE '$input%' UNION SELECT DISTINCT city,state,zip FROM `residential`  WHERE `zip` LIKE '$input%' OR `city` LIKE '$input%' OR `state` LIKE '$input%'  UNION SELECT DISTINCT state,city,zip FROM `residential` WHERE  `zip` LIKE '$input%' OR `city` LIKE '$input%' OR `state` LIKE '$input%' LIMIT $limit";

$result = mysql_query($sql);

if (!$result || !mysql_num_rows($result))
exit;

include_once "headers.php";

echo "<response>";

while ($row = mysql_fetch_array($result))
{
   $keywords = "$row[city], $row[state] $row[zip]";
   echo "<keywords>". $keywords ."</keywords>";       
}
while ($row = mysql_fetch_array($result))
{
   $keywords = "$row[city], $row[state]";
   echo "<keywords>". $keywords ."</keywords>";       
}
echo "</response>";

try

<?php
$sql = "SELECT DISTINCT zip,city,state FROM `residential` WHERE `zip` LIKE '$input%' OR `city` LIKE '$input%' OR `state` LIKE '$input%'";

$result = mysql_query($sql);

if (!$result || !mysql_num_rows($result))
exit;

include_once "headers.php";

echo "<response>";

while ($row = mysql_fetch_array($result))
{
   $keywords = "$row[city], $row[state] $row[zip]";
   echo "<keywords>". $keywords ."</keywords>";       
}
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result))
{
   $keywords = "$row[city], $row[state]";
   echo "<keywords>". $keywords ."</keywords>";       
}
echo "</response>";
?>

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.