/    Sign up×
Community /Pin to ProfileBookmark

Help with a mysql search query

I’m working on a group management system and I’m trying to fine tune the search function so it searches more table fields with a single query.

I want it to search 2 fields, something like this:

[code=php]$query = “SELECT * FROM groups WHERE name LIKE ‘%$search%’ OR description LIKE ‘%$search%’ ORDER BY name”;[/code]

Of course that works fine. But here’s the part I’m having trouble with:

I need the query to only return results for groups that this user is allowed access to (based upon a membership_id). So I need something in there such as:

[code=php]$query = “SELECT * FROM groups WHERE name LIKE ‘%$search%’ OR description LIKE ‘%$search%’ AND membership_id = $id ORDER BY name”;[/code]

Problem is the last part with the membership_id gets ignored if either of the first two parts of the query are true.

I know it can be done with 2 queries, but is it possible to keep it all in one? Thanks in advance ?

BTW, it needs to be able to work on mySQL 4 too, if possible.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ZnupiApr 13.2008 — This might help, not sure though: use parentheses:
[code=php]
query = "SELECT * FROM groups WHERE ((name LIKE '%$search%' OR description LIKE '%$search%') AND membership_id = $id) ORDER BY name";
[/code]
Copy linkTweet thisAlerts:
@jeffy777authorApr 13.2008 — Thank you, works great ?
Copy linkTweet thisAlerts:
@nonamepubApr 14.2008 — Boolean Algebra ?
Copy linkTweet thisAlerts:
@jeffy777authorApr 14.2008 — Well, I've been doing that with php if statements for years, but didn't realize it worked with mySQL too, but didn't really need it until now ?
×

Success!

Help @jeffy777 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...