w424637 Posted November 30, 2009 Share Posted November 30, 2009 Hi Can anyone explain how to use a wildcard in a query when I am retrieving numbers / values. I have a select that has options: All, 1 2 3 4 If user chooses All I want my query to bring back records where 1,2,3,4 are included else bring back just the number chosen I have tried % and it doesnt work. Here is my section of code: I need the 0 in the first section to be % effectively. Any help appreciated $colname_rs_grd = 0; if (isset($_POST['id_grade'])) { if ($_POST['id_grade'] == "All Grades") { $colname_rs_grd = 0; }else { $colname_rs_grd = $_POST['id_grade']; } mysql_select_db($database_dashboard, $dashboard); $query_rs_tbl1 = "SELECT t_gh_dash1.`WeekDay`, 'RaceCount' AS Category, Count(distinct t_gh_dash1.RaceID) as RaceCount FROM t_gh_dash1 WHERE t_gh_dash1.Pool Like '$colname_rs_pool' AND t_gh_dash1.GRD = '$colname_rs_grd' AND t_gh_dash1.trk Like '$colname_rs_trk' GROUP BY t_gh_dash1.`WeekDay`"; $rs_tbl1 = mysql_query($query_rs_tbl1, $dashboard) or die(mysql_error()); $row_rs_tbl1 = mysql_fetch_assoc($rs_tbl1); $totalRows_rs_tbl1 = mysql_num_rows($rs_tbl1); Quote Link to comment https://forums.phpfreaks.com/topic/183488-wild-card-on-values-numbers/ Share on other sites More sharing options...
ToonMariner Posted November 30, 2009 Share Posted November 30, 2009 perhaps try using t_gh_dash1.GRD IN ( $colname_rs_grd ) and set $colname_rs_grd to a comma separated list of values... Quote Link to comment https://forums.phpfreaks.com/topic/183488-wild-card-on-values-numbers/#findComment-968543 Share on other sites More sharing options...
w424637 Posted December 1, 2009 Author Share Posted December 1, 2009 Hi and thanks I can see how that would work - however my example list of 1 2 3 4 is not a fixed list so setting $colname_rs_grd to be 1,2,3,4 dosnt work - in reality the numbers are dynamic and maybe 11,456,223,etc and there maybe 10 of them or 1000? Not sure why I can't wile card numbers like using # or ? Any other thoughts? Simon Quote Link to comment https://forums.phpfreaks.com/topic/183488-wild-card-on-values-numbers/#findComment-968800 Share on other sites More sharing options...
fenway Posted December 1, 2009 Share Posted December 1, 2009 I have no idea what you're talking about. How are you storing these numbers in the database? What's your input look like? Quote Link to comment https://forums.phpfreaks.com/topic/183488-wild-card-on-values-numbers/#findComment-968858 Share on other sites More sharing options...
w424637 Posted December 1, 2009 Author Share Posted December 1, 2009 Hi Sorry if my explanation is confusing. I have a select box on my page and the user chooses either "All Values" or one of the integers in the select box. The values could be any number as they are pulled from a database. I then want to build a query which uses the value chosen above in the where statement. Select * Where t_gh_dash1.GRD = $colname_rs_grd. This works if the user chooses an integer but if I dont know how to write the query to work if the user chooses "All Values". Is it possible to write an IF statement withing my query string do basically exclude that where command if the user chooses "All Values"? Quote Link to comment https://forums.phpfreaks.com/topic/183488-wild-card-on-values-numbers/#findComment-968866 Share on other sites More sharing options...
fenway Posted December 1, 2009 Share Posted December 1, 2009 That if statement belongs in your application code, not the database -- "all values" means no where clause. Quote Link to comment https://forums.phpfreaks.com/topic/183488-wild-card-on-values-numbers/#findComment-968889 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.