brooksh Posted February 17, 2008 Share Posted February 17, 2008 How can this be accomplished? I know the code below is completely unrealistic, but how can it be done? SELECT DISTINCT City FROM listings WHERE area = 'array($_POST[area])' order by City Link to comment https://forums.phpfreaks.com/topic/91576-select-sql-from-an-array/ Share on other sites More sharing options...
darkfreaks Posted February 17, 2008 Share Posted February 17, 2008 <?php $array= array($_POST['area']); $sql="SELECT DISTINCT City FROM listings WHERE area=$array ORDER BY City"; mysql_query($sql);?> Link to comment https://forums.phpfreaks.com/topic/91576-select-sql-from-an-array/#findComment-469055 Share on other sites More sharing options...
Barand Posted February 17, 2008 Share Posted February 17, 2008 If $_POST['area'] is an array of areas eg array('x', 'y', 'z') then <?php $areas = join ("','", $_POST['area']); $sql="SELECT DISTINCT City FROM listings WHERE area IN ('$areas')"; Link to comment https://forums.phpfreaks.com/topic/91576-select-sql-from-an-array/#findComment-469066 Share on other sites More sharing options...
darkfreaks Posted February 17, 2008 Share Posted February 17, 2008 <?php $areas = join ("','", $_POST['area']); $nested_array=serialize($areas); $make_safe_query=mysql_real_escape_string($nested_array); $sql="SELECT DISTINCT City FROM listings WHERE area IN ('$areas')";?> adding onto it. Link to comment https://forums.phpfreaks.com/topic/91576-select-sql-from-an-array/#findComment-469069 Share on other sites More sharing options...
Barand Posted February 17, 2008 Share Posted February 17, 2008 Why serialize a string? Link to comment https://forums.phpfreaks.com/topic/91576-select-sql-from-an-array/#findComment-469071 Share on other sites More sharing options...
brooksh Posted February 17, 2008 Author Share Posted February 17, 2008 I am running php 4.3 and I don't know if that's why I'm getting this error. Warning: join() [function.join]: Bad arguments. Link to comment https://forums.phpfreaks.com/topic/91576-select-sql-from-an-array/#findComment-469078 Share on other sites More sharing options...
darkfreaks Posted February 17, 2008 Share Posted February 17, 2008 <?php $areas = array( $_POST['area']); $comma_seperated=implode(",",$areas); $nested_array=serialize($comma_seperated); $make_safe_query=mysql_real_escape_string($nested_array); $sql="SELECT DISTINCT City FROM listings WHERE area IN ('$make_safe_query')"; mysql_query($sql); ?> Link to comment https://forums.phpfreaks.com/topic/91576-select-sql-from-an-array/#findComment-469079 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.