wemustdesign Posted April 10, 2009 Share Posted April 10, 2009 I am passing a query string which is used to query my database. The problem I am having is that I don't know how to 'activate' the appropriate query when certain variables are passed. For example, if the user clicks on a region: //index.php?region=$region_id $data_p = mysql_query("SELECT * FROM listings WHERE region = '$region_id' $max") or if a user clicks on a city within a region: //index.php?region=$region_id&city=$city_id $data_p = mysql_query("SELECT * FROM listings WHERE region = '$region_id' AND city ='$city_id' $max") or die(mysql_error()); or if a user clicks on a county within a region //index.php?region=$region_id&county=$county_id $data_p = mysql_query("SELECT * FROM listings WHERE region = '$region_id' AND county ='$county_id' $max") or if nothing is clicked show all of the listings //index.php $data_p = mysql_query("SELECT * FROM listings $max") I have tried creating an if statement using a range of techniques such as: if (!isset($city_id)) if ($city_id == null) but can't get it to work. Can anyone point me in the right direction of what I should be doing? Thanks, Chris Link to comment https://forums.phpfreaks.com/topic/153442-solved-query-from-a-query-string/ Share on other sites More sharing options...
mmarif4u Posted April 10, 2009 Share Posted April 10, 2009 If i am getting your question correctly, you can use GET method. //index.php?region=$region_id $region_id1 = $_GET['region']; $data_p = mysql_query("SELECT * FROM listings WHERE region = '$region_id1' $max") Is this what, you are looking for. Link to comment https://forums.phpfreaks.com/topic/153442-solved-query-from-a-query-string/#findComment-806195 Share on other sites More sharing options...
wemustdesign Posted April 10, 2009 Author Share Posted April 10, 2009 Yes I am already using this method to retrieve the variables. The problem I am having is executing the appropriate query depending on what query string is passed. for example if $region and $city or passed query 1 will execute $region and $county are passed query 2 will execute and so on, if you get what I mean Link to comment https://forums.phpfreaks.com/topic/153442-solved-query-from-a-query-string/#findComment-806201 Share on other sites More sharing options...
mmarif4u Posted April 10, 2009 Share Posted April 10, 2009 ok, i get your question. You want some sort of checking statement. You are right about if else statement. You can it try it like: if($region != '' && $city != '' ){ Execute 1st query } elseif($region !='' && $country != ''){ execute 2nd query } Hope, this will give you some insight. Link to comment https://forums.phpfreaks.com/topic/153442-solved-query-from-a-query-string/#findComment-806204 Share on other sites More sharing options...
Mark Baker Posted April 10, 2009 Share Posted April 10, 2009 $query = "SELECT * FROM listings WHERE 1=1"); if ($_GET['region'] > '') { $query .= " AND region = '$region_id'"; } if ($_GET['city'] > '') { $query .= " AND city ='$city_id'"; } $query .= " $max"; $data_p = mysql_query($query); Remember to check that your $_GET variables are security checked Link to comment https://forums.phpfreaks.com/topic/153442-solved-query-from-a-query-string/#findComment-806208 Share on other sites More sharing options...
wemustdesign Posted April 10, 2009 Author Share Posted April 10, 2009 Superb thanks for your help, have been pulling my hair out tring to get this to work. Gonna try this now. Thanks again! Link to comment https://forums.phpfreaks.com/topic/153442-solved-query-from-a-query-string/#findComment-806209 Share on other sites More sharing options...
wemustdesign Posted April 10, 2009 Author Share Posted April 10, 2009 Cheers Mark worked a treat, like your little WHERE 1=1 trick would never have thought of doing that. Cheers Link to comment https://forums.phpfreaks.com/topic/153442-solved-query-from-a-query-string/#findComment-806213 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.