【问题标题】:MySQL search case insensitiveMySQL 搜索不区分大小写
【发布时间】: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


【解决方案1】:
/* 
 * 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 .= "UPPER(`".$aColumns[$i]."`) LIKE '%".strtoupper(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 .= "UPPER(`".$aColumns[$i]."`) LIKE '%".strtoupper(mysql_real_escape_string($_GET['sSearch_'.$i]))."%' ";
    }
}

【讨论】:

    【解决方案2】:
    WHERE UPPER(column_name) LIKE '%" . strtoupper(string). "%'
    

    【讨论】:

    • 在我的代码中在哪里添加“上部”?这段代码对我来说似乎太高级了:D
    【解决方案3】:

    如果您有一个不区分大小写的排序规则(通常是默认排序规则),那么您的搜索将默认不区分大小写。

    【讨论】:

    • 不确定我明白你的意思..但它似乎是区分大小写的
    • @user1741397 您尝试搜索的字段的当前排序规则是什么?
    【解决方案4】:

    试试这个:

    /* 
     * 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 .= "UCASE(`".$aColumns[$i]."`) LIKE '%UCASE(".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 .= "UCASE(`".$aColumns[$i]."`) LIKE '%UCASE(".mysql_real_escape_string($_GET['sSearch_'.$i]).")%' ";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多