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>";

Link to comment
Share on other sites

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>";
?>

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.