glennn.php Posted September 9, 2008 Share Posted September 9, 2008 ok, sorry for my ignorance, but i'm curious about whether to do one of two things: Given a form with about 10 choices, many (all) of which might return an array - $offering_sql might contain a few values, or it might contain none. if it contains none, then this selects ALL the values in that table field, i'm pretty sure that's obvious (not sure of anything today). my query might be built like so: SELECT fice FROM characteristics WHERE $offering_sql AND $public_private_sql AND $variable AND ... My question is, considering that some values will be empty, is it better to do this: if ($offerings != '') { $offering_sql = "offering IN ($offering_sql)"; } else { if ($offerings == '') { $offering_sql = "offering LIKE '%'"; } or this: if ($offerings != '') { $offering_sql = "offering IN ($offering_sql) AND"; } else { if ($offerings == '') { $offering_sql = ""; } in other words, IF ONLY ONE FORM CATEGORY OF TEN IS CONTAINS VALUES, should my query be: SELECT fice FROM characteristics WHERE offering LIKE '%' AND public_private LIKE '%' AND (value) LIKE '%' AND (value) LIKE '%' AND (value) LIKE '%' AND (value) LIKE '%' ... or should i configure those if statements to return '' if i can... does it matter either way? don't know what to consider while considering these choices... thanks for anyone's help, GN Quote Link to comment https://forums.phpfreaks.com/topic/123476-solved-including-variables-in-a-query-even-if-theyre-empty/ Share on other sites More sharing options...
sasa Posted September 9, 2008 Share Posted September 9, 2008 try $where = array(); if ($offerings != '') { $where[] = "offering IN ($offering_sql)"; } if ($some_field != '') { $where[] = "some_field IN ($some_field_sql)"; } //etc. $where = count($where) ? implode(' AND ', $where) : 1; $sql = "SELECT fice FROM characteristics WHERE $where"; Quote Link to comment https://forums.phpfreaks.com/topic/123476-solved-including-variables-in-a-query-even-if-theyre-empty/#findComment-637747 Share on other sites More sharing options...
glennn.php Posted September 9, 2008 Author Share Posted September 9, 2008 this returns Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/coburncr/public_html/hepinc/admin_characteristics.php on line 405 this is returning no values no matter how many options are chosen in the form. "implode(',',$offering);" would be something like 03,04,05 $offering = $_POST['highest_deg']; if ($offering != '') { $offerings = implode(',',$offering); } else { if ($offering == '') { $offerings = ''; } } $where = array(); if ($offerings != '') { $where[] = "offering IN ($offering_sql)"; } $where = count($where) ? implode(' AND ', $where) : 1; $where holds "offering IN ()" no matter what. i'm trying to get it to EITHER have "offering IN (03,04,05) AND" OR ""... Quote Link to comment https://forums.phpfreaks.com/topic/123476-solved-including-variables-in-a-query-even-if-theyre-empty/#findComment-637792 Share on other sites More sharing options...
sasa Posted September 9, 2008 Share Posted September 9, 2008 in implode you create variable $offerings and in 2nd part you use $offering_sql Quote Link to comment https://forums.phpfreaks.com/topic/123476-solved-including-variables-in-a-query-even-if-theyre-empty/#findComment-637799 Share on other sites More sharing options...
glennn.php Posted September 9, 2008 Author Share Posted September 9, 2008 oops - right - was playing with those conditions and overlooked that. i think that worked. Quote Link to comment https://forums.phpfreaks.com/topic/123476-solved-including-variables-in-a-query-even-if-theyre-empty/#findComment-637812 Share on other sites More sharing options...
glennn.php Posted September 9, 2008 Author Share Posted September 9, 2008 awesome - thanks. i see how that works, now... beautiful, thanks. GN Quote Link to comment https://forums.phpfreaks.com/topic/123476-solved-including-variables-in-a-query-even-if-theyre-empty/#findComment-637854 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.