简体   繁体   中英

mysql_query select from multiple columns with OR

I have:

$sql = mysql_query("select * from articles where articletitle OR articledescription OR articletags like '%$term%'");

and it returns no results.

Am I using OR incorrectly here?

Thanks

您将需要在所有三个子句中添加LIKE:

SELECT * FROM articles WHERE (articletitle LIKE '%$term%') OR (articledescription LIKE '%$term%') OR (articletags LIKE '%$term%');

Nope.. you need to use the like '...' everytime

$sql = mysql_query("select * from articles where articletitle like '%$term%' OR 
articledescription like '%$term%' OR articletags like '%$term%'");

Yes. :-) You're using it incorrectly.

select * from articles where (articletitle like '%$term%') 
OR (articledescription like '%$term%') OR (articletags like '%$term%')

On a side comment, what would happen if $term is exactly (minus double quotes) "';DELETE FROM articles;-- "? (Add a closing paren after the single quote if your original query had such.) I recommend you rely on some sprintf-like facility (in PHP it often means you'll need prepared statements) rather than the deprecated misfeature known as magic quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM