Jump to content

[SOLVED] Select * from table where value = this or that


clay1

Recommended Posts

I have a form which produces an postgresql query. It selects all the records where the event city = the posted variable

 

As such:

 

	  $sql = "SELECT * from \"leads\" WHERE \"event city\" ~* '{$_POST['select']}' and \"date\" {$_POST['operator']} '{$y1}-{$m1}-{$d1}'";

 

This works fine-- however sometimes I need to select more than just the posted variable. For example if the post is washington dc, I also want the query to select alexandria

 

I tried:

 

  if ($_POST['select'] == 'washington dc'){
  
$sql = "SELECT * from \"leads\" WHERE \"event city\" ~* '{$_POST['select']}' OR 'alexandria' and \"date\" {$_POST['operator']} '{$y1}-{$m1}-{$d1}'";

 

But I get an invalid syntax error.

 

How can  I do this?

Its just like PHP in that each condiftion must stand on it's own. You can't do where x = 1 or 2, you have to do where x = 1 or x = 2.

 

I don't use postgresql, but this should be right

  if ($_POST['select'] == 'washington dc'){
  
$sql = "SELECT * from \"leads\" WHERE \"event city\" ~* '{$_POST['select']}' OR \"event city\" ~* 'alexandria' and \"date\" {$_POST['operator']} '{$y1}-{$m1}-{$d1}'";

Thanks! That worked the only thing I needed to change was it needs the date for both

 

$sql = "SELECT * from \"leads\" WHERE \"event city\" ~* '{$_POST['select']}'  and \"date\" {$_POST['operator']} '{$y1}-{$m1}-{$d1}' OR \"event city\" ~* 'alexandria' and \"date\" {$_POST['operator']} '{$y1}-{$m1}-{$d1}'";

 

 

Thanks a lot

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.