Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.