Jump to content

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.

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.