Jump to content

A problem retreiving data from the databse


npsari

Recommended Posts

Hi users!

 

I use this query to fetch results...

 

$q = "SELECT * FROM profile WHERE Country='$Country' AND Gender='$Gender' AND Age='$Age' ORDER BY Date DESC";

 

But users sometime select Any country, or Any age, etc..

 

How can i cancel  Country='$Country'  or Gender='$Gender' etc..  if a user chose Any

 

I can't use if function because the possibilities are endless

Oh ok. I misread your question the first time.

 

Try an OR statement wrapped in ()s or something.

 

$q = "SELECT * FROM profile WHERE (Country='$Country' OR Country='Any') AND Gender='$Gender' AND (Age='$Age' OR Age='Any') AND Country!='Any' AND Age!='Any' ORDER BY Date DESC";

 

 

 

Hello,

 

You need to check whether the user selects any country and creates SQL query according to that.

 

For Example:

 

if($_GET['country'] == "Any")

  $countrySql  = "";

else

$countrySql = " AND Country= '".$_GET['country']."' ";

 

 

Create sql query for all of the drop downs and finally make your sql statement as:

 

$q = "SELECT * FROM profile WHERE 1 $countrySql ";

 

 

Regards

Aniesh Joseph

[email protected]

Thanks charlieholder  for your reply

 

However, your above code will search for a Country called Any

 

I dont want that actually

 

I want when a users selects Any... The database simply shows all the countries

 

but if the user choses USA, then, database searches only for USA

So can i say...

 

if($_GET['country'] == "Any")

   $countrySql  = "";

else

$countrySql = " AND Country= '".$_GET['country']."' ";

 

if($_GET['age'] == "Any")

   $ageSql  = "";

else

$ageSql = " AND Age= '".$_GET['age']."' ";

 

$q = "SELECT * FROM profile WHERE 1 $countrySql AND $ageSql ";

 

is that ok?

and What is the 1 by the way?

 

Hello

 

A slight changes on your query . Please remove the "AND". Because we already added AND  in $countrySql and $ageSql.

 

$q = "SELECT * FROM profile WHERE 1 $countrySql  $ageSql ";

 

Suppose that at the first time you calling the page, do not have any "country or age " in the URL . So if you omit the 1 , the query will looks like this:

 

$q = "SELECT * FROM profile WHERE " ; /*  Nothing after the WHERE */

 

I think you got the idea now.

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.