php - MySQL搜索不区分大小写

标签 php mysql server-side-scripting

我有一个代码,它负责搜索字段来搜索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])."%' ";
    }
}

最佳答案

/* 
 * 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]))."%' ";
    }
}

关于php - MySQL搜索不区分大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18921221/

相关文章:

html - 有没有办法给 HTML 输入某种命名空间来避免冲突?

php - 有 PHP -> jQuery 库吗?

mysql - Zend_Db_Expr ('NOW()' )和 Mysql 中的日期时间

php - mySQL 排序和不同

php - 登录 session PHP

mysql:复制数据或 LIKE 子句

javascript - 使用 jQuery 在服务器端处理数据表

php - 通过循环 AJAX 或服务器端调用繁重的 PHP 函数

php - 如何使用 php 获取网站的 css

php - Sybase Adaptive Server IQ 无法 SELECT *,始终限制为 30?