繁体   English   中英

服务器端处理数据表中的 UTF-8 搜索

[英]Server-side processing UTF-8 search in DataTables

我将 DataTables 与服务器端处理一起使用

$('#usersTable').DataTable(
            {
                responsive: true,
                "pageLength": 20,
                "processing": true,
                "serverSide": true,
                "bLengthChange": true,
                "bSort" : false,
                "bInfo" : false,
                "aLengthMenu": [[20, 50, 75, -1], [20, 50, 75, "ყველა"]],
                "ajax": "helpers/server_processing.php"
            }
        );

我还将ssp.class.php中的数据库连接ssp.class.php

$db = @new PDO(
"mysql:host={$sql_details['host']};dbname={$sql_details['db']}",
$sql_details['user'],
$sql_details['pass'],
array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'" )
);

尝试在搜索栏中搜索 UTF-8 字符时仍然遇到问题

数据表错误

有人可以帮忙吗?

查询(从评论中复制)

SELECT  COUNT(`id`)
    FROM  `users`
    WHERE  (`id` LIKE :binding_0
              OR  `mac` LIKE :binding_1
              OR  `ip` LIKE :binding_2
              OR  `sname` LIKE :binding_3
              OR  `login` LIKE :binding_4
              OR  `tariff_plan_id` LIKE :binding_5
              OR  `now_playing_type` LIKE :binding_6
              OR  `now_playing_content` LIKE :binding_7
              OR  `now_playing_start` LIKE :binding_8
              OR  `keep_alive` LIKE :binding_9
              OR  `id` LIKE :binding_10
              OR  `status` LIKE :binding_11
           ) 
SELECT  COUNT(`id`)
    FROM  `users`
SELECT  `id`, `mac`, `ip`, `sname`, `login`, `tariff_plan_id`,
        `now_playing_type`, `now_playing_content`, `now_playing_start`,
        `keep_alive`, `id`, `status`
    FROM  `users`
    WHERE  (`id` LIKE :binding_0
              OR  `mac` LIKE :binding_1
              OR  `ip` LIKE :binding_2
              OR  `sname` LIKE :binding_3
              OR  `login` LIKE :binding_4
              OR  `tariff_plan_id` LIKE :binding_5
              OR  `now_playing_type` LIKE :binding_6
              OR  `now_playing_content` LIKE :binding_7
              OR  `now_playing_start` LIKE :binding_8
              OR  `keep_alive` LIKE :binding_9
              OR  `id` LIKE :binding_10
              OR  `status` LIKE :binding_11
           )
    ORDER BY  id ASC
    LIMIT  0, 20

编辑:附加信息

我发现使用 utf-8 值的 LIKE 查询不适用于 mysql 中的 DateTime 字段在此处输入图片说明

但是数据表会自动将每个字段与搜索字符串进行比较。 如果字符串包含 utf-8 字符,是否有一种简单的方法可以告诉 DataTables 不要搜索 DateTime 类型的列?

使用new PDO('dblib:host=host;dbname=db;charset=UTF8', $user, $pwd); 而不是ATTR_INIT_COMMAND

->set_charset用于mysqli接口,而不是PDO

根据图像,列是CHARACTER SET utf8 COLLATION utf8_general_ci ,这对于格鲁吉亚语来说已经足够了。

向我们展示带有抱怨的LIKE的查询。

问题与LIKE语句有关。 如果它在存储例程中,让我们看看SHOW CREATE ... ; 可能是错误的字符集/排序规则在创建时生效。

由于它的“最佳实践”一节中说这个,“HTML表单应该开始喜欢”。 检查那里的其他项目。

尝试这个:

$db_link = new PDO($dsn, $username, $password) // DB-connection

// CHARSET: utf8
$db_link->query('SET NAMES utf8');

如果搜索字符串为 utf-8,我通过更改ssp.class.php filter功能以从搜索中ssp.class.php DateTime 列来解决该问题

static function filter ( $request, $columns, &$bindings )
{
    $globalSearch = array();
    $columnSearch = array();
    $dtColumns = self::pluck( $columns, 'dt' );

    if ( isset($request['search']) && $request['search']['value'] != '' ) {
        $str = $request['search']['value'];

        for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
            $requestColumn = $request['columns'][$i];
            $columnIdx = array_search( $requestColumn['data'], $dtColumns );
            $column = $columns[ $columnIdx ];

            //**ADDED THIS**
            if(mb_detect_encoding($request["search"]["value"])=="UTF-8"){
                if($column['db']=="keep_alive" || $column['db']=="now_playing_start"){
                    continue;
                }
            }

            if ( $requestColumn['searchable'] == 'true' ) {
                $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                $globalSearch[] = "`".$column['db']."` LIKE ".$binding;
            }
        }
    }

    // Individual column filtering
    if ( isset( $request['columns'] ) ) {
        for ( $i=0, $ien=count($request['columns']) ; $i<$ien ; $i++ ) {
            $requestColumn = $request['columns'][$i];
            $columnIdx = array_search( $requestColumn['data'], $dtColumns );
            $column = $columns[ $columnIdx ];

            $str = $requestColumn['search']['value'];

            if ( $requestColumn['searchable'] == 'true' &&
                $str != '' ) {
                $binding = self::bind( $bindings, '%'.$str.'%', PDO::PARAM_STR );
                $columnSearch[] = "`".$column['db']."` LIKE ".$binding;
            }
        }
    }

    // Combine the filters into a single string
    $where = '';

    if ( count( $globalSearch ) ) {
        $where = '('.implode(' OR ', $globalSearch).')';
    }

    if ( count( $columnSearch ) ) {
        $where = $where === '' ?
            implode(' AND ', $columnSearch) :
            $where .' AND '. implode(' AND ', $columnSearch);
    }

    if ( $where !== '' ) {
        $where = 'WHERE '.$where;
    }

    return $where;
}

暂无
暂无

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

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