Jump to content

Question about 'Where' Statement


yakk0

Recommended Posts

$result = mysql_query("SELECT * FROM members WHERE Clan='Zenith' ORDER BY Level DESC,Members ASC");

 

I would like to know if I can add more then one key word when Its using "Where" Statement. Like I want it to search for Clan='Zenith' but also search for another Clan='something'. Would it be possible for it to search more then one key word and list them all in one?

Link to comment
https://forums.phpfreaks.com/topic/133347-question-about-where-statement/
Share on other sites

Either the IN operator or the OR operator.

 

IE:

$result = mysql_query("SELECT * FROM members WHERE Clan='Zenith' OR Clan='something' OR Clan='somethingelse'ORDER BY Level DESC,Members ASC");

OR

$result = mysql_query("SELECT * FROM members WHERE Clan IN('Zenith', 'something', 'somethingelse') ORDER BY Level DESC,Members ASC");

I didn't know about the IN statement, that's a nice typing saver!

 

Works even better if your clan name list is in an array.

 

$clanname = array("Zenith", "Exodus", "Paradon");
$clans = "'" . implode("', '", $clanname) . "'";
$result = mysql_query("SELECT * FROM members WHERE Clan IN(" . $clans . ") ORDER BY Level DESC,Members ASC");

 

Pretty fancy stuff =)

 

EDIT: A different way to do the above

 

$clanname = array("Zenith", "Exodus", "Paradon");
$clans = implode("', '", $clanname);
$result = mysql_query("SELECT * FROM members WHERE Clan IN('" . $clans . "') ORDER BY Level DESC,Members ASC"); // note the added single quotes inside the in

 

That also works well, just thought of it so figured I would also post it. Without the singlequotes MySQL would throw an error.

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.