paddy_fields Posted January 2, 2011 Share Posted January 2, 2011 hi, happy new year. i am using an html form and checkboxes so a user can select one or more genres they want returned in a search. the values are sent via GET under the name genre[] so they are stored as an array. however, how do i use this with my sql query to select records that consist of any of the values of the array, eg... searching for rock, pop, dance will return any relevant events. i can assign each array value to it's own variable, but then i don't see how this works with SQL if i don't already know the number of genres being searched for. headache. any advice would be great. Pat. Quote Link to comment https://forums.phpfreaks.com/topic/223205-php-arrays-and-sql-select/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 2, 2011 Share Posted January 2, 2011 You would generally use the mysql IN() operator to match a column with several values - http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#function_in You would produce the string used in the IN(...) by imploding the array of selected values. <?php $_GET['genre'][] = 'rock'; // simulate some data $_GET['genre'][] = 'pop'; $_GET['genre'][] = 'dance'; $genres = "'".implode("','",$_GET['genre'])."'"; $query = "SELECT * FROM your_table WHERE genre IN($genres)"; echo $query; ?> Quote Link to comment https://forums.phpfreaks.com/topic/223205-php-arrays-and-sql-select/#findComment-1153893 Share on other sites More sharing options...
paddy_fields Posted January 2, 2011 Author Share Posted January 2, 2011 web five. Quote Link to comment https://forums.phpfreaks.com/topic/223205-php-arrays-and-sql-select/#findComment-1153894 Share on other sites More sharing options...
paddy_fields Posted January 2, 2011 Author Share Posted January 2, 2011 ah, can't seem to get it working. it is telling me that this is an invalid sql argument. <?php include("connect.php"); $_GET['genre'][] = 'rock'; // simulate some data $_GET['genre'][] = 'pop'; $_GET['genre'][] = 'dance'; $genres = "'".implode("','",$_GET['genre'])."'"; $query = mysql_query("SELECT * FROM gig WHERE genre IN('$genres')"); $row = mysql_fetch_array($query); echo $row['gigid']; ?> am i being stupid? Quote Link to comment https://forums.phpfreaks.com/topic/223205-php-arrays-and-sql-select/#findComment-1153915 Share on other sites More sharing options...
paddy_fields Posted January 2, 2011 Author Share Posted January 2, 2011 yes, i was. my sql value is called eventgenre, not genre. Quote Link to comment https://forums.phpfreaks.com/topic/223205-php-arrays-and-sql-select/#findComment-1153918 Share on other sites More sharing options...
PFMaBiSmAd Posted January 2, 2011 Share Posted January 2, 2011 Not intentionally picking on you, but we only see the information that is supplied in posts and any code that is posted in a reply is just an example that shows how you might do something and comes with an implied disclaimer that it may or may not have been tested, could contain typos, syntax errors, logic errors, other oversights, and may or may not be suitable for any particular purpose. There's no guarantee expressed or implied and your actual mileage may vary. Quote Link to comment https://forums.phpfreaks.com/topic/223205-php-arrays-and-sql-select/#findComment-1153929 Share on other sites More sharing options...
paddy_fields Posted January 3, 2011 Author Share Posted January 3, 2011 i am aware of that. i was using example you gave me to test the concept. Quote Link to comment https://forums.phpfreaks.com/topic/223205-php-arrays-and-sql-select/#findComment-1154151 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.