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
https://forums.phpfreaks.com/topic/210969-mysql-query/
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
https://forums.phpfreaks.com/topic/210969-mysql-query/#findComment-1100374
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
https://forums.phpfreaks.com/topic/210969-mysql-query/#findComment-1100382
Share on other sites

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.