Jump to content

[SOLVED] Query from a Query String


wemustdesign

Recommended Posts

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

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.

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

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.

$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.