简体   繁体   English

mysql_query使用OR从多个列中选择

[英]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? 我在这里使用OR错误吗?

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;-- "? 附带说明一下,如果$ term恰好是(减去双引号)“';从文章中删除;-”会发生什么? (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. (如果原始查询有单引号,请在单引号后添加一个结束括号。)我建议您依靠某种类似于sprintf的功能(在PHP中,这通常意味着您需要准备好的语句),而不要使用已弃用的功能,即魔术引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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