【发布时间】:2013-09-26 02:30:35
【问题描述】:
我有一个代码,它负责在 mysql 表中搜索数据的搜索字段。 问题是它的大小写敏感,我查看了作者网站和他们建议从 LIKE 更改为 ILIKE 的地方,但这会导致搜索不起作用
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
{
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
}
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
{
if ( $sWhere == "" )
{
$sWhere = "WHERE ";
}
else
{
$sWhere .= " AND ";
}
$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
}
}
【问题讨论】:
-
您应该使用 PDO 或 MySQLi 和准备好的语句而不是 mysql_ 函数,它们已被弃用。
-
更改表格以使用不区分大小写的排序规则。 MySQL 的普通表默认值(和比较)不区分大小写。区分大小写通常是您专门为系统配置的内容。
标签: php mysql server-side-scripting