brooksh Posted October 19, 2010 Share Posted October 19, 2010 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 https://forums.phpfreaks.com/topic/216305-help-selecting-distinct-values-from-3-columns/ Share on other sites More sharing options...
sasa Posted October 20, 2010 Share Posted October 20, 2010 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 https://forums.phpfreaks.com/topic/216305-help-selecting-distinct-values-from-3-columns/#findComment-1124270 Share on other sites More sharing options...
brooksh Posted October 20, 2010 Author Share Posted October 20, 2010 seems to work. Thanks. Link to comment https://forums.phpfreaks.com/topic/216305-help-selecting-distinct-values-from-3-columns/#findComment-1124274 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.