Jump to content

Mysql query


zero_ZX

Recommended Posts

Hello,

I have a query where i try to search, but i want to put a limitation, but it doesn't seems to be working :/

Here's my code:

$result = mysql_query("SELECT * FROM clients WHERE staff = 0 AND username LIKE '%" . $keyword .  "%' OR company LIKE '%" . $keyword .  "%' ORcontact LIKE '%" . $keyword .  "%' OR address LIKE '%" . $keyword .  "%' OR email LIKE '%" . $keyword .  "%' OR phone LIKE '%" . $keyword .  "%' ORDER BY phone");

 

Alright, so i basically want the user to search in all of those fields, however i want it to filter all "staff" members, so it should only view the "0" ones, meaning the clients, only problem that when i run this, all clients gets displayed, also the staff accounts.

 

Any suggestion?

Link to comment
Share on other sites

If you want a specific value AND any of the other values, you must () to indicate that's what you want -

"SELECT * FROM clients WHERE staff = 0 AND (username LIKE '%" . $keyword .  "%' OR company LIKE '%" . $keyword .  "%' OR contact LIKE '%" . $keyword .  "%' OR address LIKE '%" . $keyword .  "%' OR email LIKE '%" . $keyword .  "%' OR phone LIKE '%" . $keyword .  "%' ORDER BY phone)"

Link to comment
Share on other sites

Oh ya that's right, thanks a lot!

Remember that order by shouldn't be, so the final code would be

"SELECT * FROM clients WHERE staff = 0 AND (username LIKE '%" . $keyword .  "%' OR company LIKE '%" . $keyword .  "%' OR contact LIKE '%" . $keyword .  "%' OR address LIKE '%" . $keyword .  "%' OR email LIKE '%" . $keyword .  "%' OR phone LIKE '%" . $keyword .  "%') ORDER BY phone"

 

Link to comment
Share on other sites

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.