Hello,

I have a query that needs to perform the following:

where verified=1
AND
where name is like '%$term%' AND/OR owner is like '%$term%'

I have a basic knowledge of SQL and was wondering if the above is possible.

I need the and/or because i would like it to return a result whether only a name was found, just an owner, or both.


Please help me :)

--Turt2Live

Recommended Answers

All 6 Replies

...i would like it to return a result whether only a name was found, just an owner, or both.

You don't need to AND. The OR should give you what you want even when both are true:

$sql="SELECT `name`, `owner` 
FROM Table WHERE `name` LIKE '$%term%' OR `owner` LIKE '%$term%'";

Ok, so if I were to use the following, would it still work?

$sql = "SELECT `name`,`owner` FROM table WHERE `verified`='1' AND name LIKE '$%term%' OR `owner` LIKE '%$term%'";

try this

$sql = "SELECT 'name','owner' FROM table WHERE 'verified'='1' AND (name LIKE '$%term%' OR 'owner' LIKE '%$term%')";

It works! Thank you very much :D

Ok, so if I were to use the following, would it still work?

$sql = "SELECT `name`,`owner` FROM table WHERE `verified`='1' AND name LIKE '$%term%' OR `owner` LIKE '%$term%'";

No! In that specific example, the query is treated as:

...
(`verified`='1' AND name LIKE '$%term%')
OR owner LIKE '%$term%'

In your case you clearly want the OR name with owner, so you must parenthesize that expression:

...`verified`='1' AND 
(name LIKE '$%term%' OR owner LIKE '%$term%')

NOTE: In most of these replies change: name LIKE '$%term%' to: name LIKE '%$term%'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.