Jump to content

SELECTing multiple values from a field


Tenaciousmug

Recommended Posts

Ok I'm trying to select the pet with the current eggid in the url. That part works fine, but when I try to select more than one color, it gets all messed up. Because I don't know how to do it and I can't find it on the internet anywhere.

 

$sql = "SELECT * FROM pets WHERE eggid='".$_GET['eggid']."' AND color='blue' OR 'green' OR 'yellow' OR 'red'";

 

Does anyone know how to do that? Because whenever I put this:

 

$sql = "SELECT * FROM pets WHERE eggid='".$_GET['eggid']."' AND color='blue' OR color='yellow' OR color='green' OR color='red'";

 

It gets all messed up. It selects ALL the colors even if the eggid isn't the current eggid. So it displays the colors of all the pets that have those colors.. :/

 

 

EDIT:

 

OR is this the only way I can do it:

$sql = "SELECT * FROM pets WHERE eggid='".$_GET['eggid']."' AND color='blue' OR eggid='".$_GET['eggid']."' AND color='green' OR eggid='".$_GET['eggid']."' AND color='yellow' OR eggid='".$_GET['eggid']."' AND color='red'";

Link to comment
https://forums.phpfreaks.com/topic/236376-selecting-multiple-values-from-a-field/
Share on other sites

the OR clauses are overriding your eggid.

$sql = "SELECT * FROM pets WHERE eggid='".$_GET['eggid']."' AND (color='blue' OR color='yellow' OR color='green' OR color='red')";

you need to group your conditions in an sql 'where' clause just the same as in an 'if'  conditional.

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.