如何在 MySQL 的 instr() 中应用通配符? [英] How to apply wildcard in instr() in MySQL?
本文介绍了如何在 MySQL 的 instr() 中应用通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用通配符以便匹配所有记录.
Using a wildcard so all records will be matched.
我的代码:
if(empty($tag))
{
$tag="%";
}
mysql_query("select * from mytable where instr(tag,'$tag')>0") or die(mysql_error());
但它返回零结果.即使
if(empty($tag))
{
$tag="*";
}
它仍然返回零结果.如何解决这个问题?
It still returns zero result. How to resolve this problem?
推荐答案
INSTR
不支持通配符.
如果要在 MySQL 查询中使用通配符,则必须使用支持它的函数,例如 LIKE 或 REGEXP,即:
If you want to use wildcards in a MySQL query, you'll have to use a function that supports it, such as LIKE or REGEXP, ie:
mysql_query("select * from mytable where tag LIKE '%$tag%'")
上述查询适用于 $tag
的任何值.空白值(空字符串)将返回所有记录.确保您也正确清理了 $tag
值.
The above query will work for any value of $tag
. A blank value (empty string) will return all records. Be sure you properly sanitize your $tag
value as well.
这篇关于如何在 MySQL 的 instr() 中应用通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文