Mysql web应用程序中的Unicode支持(包括表情符号),不区分重音的排序规则

Mysql web应用程序中的Unicode支持(包括表情符号),不区分重音的排序规则,mysql,perl,unicode,Mysql,Perl,Unicode,我有一个使用Perl、CGI和MySQL 5.5.62的遗留web应用程序。在客户填写的字段中,我需要支持他们在输入中经常使用的umlaut和表情符号 为了学习,我设置了以下独立测试。它故意非常简单,并且缺少对输入的基本安全检查 数据库小部件的转储: Perl代码: #!/usr/bin/perl -T use strict; use warnings; use DBI; use CGI '-utf8'; my $dbh = DBI->connect('DBI:mysql:widge

我有一个使用Perl、CGI和MySQL 5.5.62的遗留web应用程序。在客户填写的字段中,我需要支持他们在输入中经常使用的umlaut和表情符号

为了学习,我设置了以下独立测试。它故意非常简单,并且缺少对输入的基本安全检查

数据库小部件的转储:

Perl代码:

#!/usr/bin/perl -T

use strict;
use warnings;

use DBI;
use CGI '-utf8';

my $dbh = DBI->connect('DBI:mysql:widget','test','test', { mysql_enable_utf8 => 0,}) or die "Can't connect to the database: $DBI::errstr";
my $sth = $dbh->prepare('SELECT * FROM `experiment`') or die "Couldn't prepare statement: " . $dbh->errstr;
$sth->execute or die "Can't execute SQL statement: $DBI::errstr";
my $hashref = $sth->fetchrow_hashref or die "Can't fetchrow_hashref: $DBI::errstr\n";
$sth->finish;
my $search = '';
for my $i (qw(fox dog)) {
    $sth = $dbh->prepare("SELECT * FROM `experiment` WHERE `content` LIKE '%$i%'") or die "Couldn't prepare statement: " . $dbh->errstr;
    my $count = $sth->execute or die "Can't execute SQL statement: $DBI::errstr";
    $search .= "<h6>String: [$i] found [$count]</h6>";
}
$sth->finish;

my $action = CGI::param('action') || '';
if ($action eq 'save') {
    my $new = CGI::param('value') || '';
    $sth = $dbh->prepare("UPDATE `experiment` SET `content` = '$new' WHERE `id` = 1") or die "Couldn't prepare statement: " . $dbh->errstr;
    $sth->execute or die "Can't execute SQL statement: $DBI::errstr";
    $sth->finish;
    print "Location: http://simulated-domain-name.com/cgi-bin/test.cgi\n\n";
    exit;
}
$dbh->disconnect;
print <<EOF;
Content-type: text/html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <!-- not part of the experiment, just make it look nice -->
    </head>
    <body>
        <div class="container my-3">
            <h5>Content = $hashref->{content}</h5>
$search
            <form method="post">
                <input type="hidden" name="action" value="save">
                <div class="form-group">
                    <label class="font-weight-bold" for="exampleFormControlTextarea1">Content</label>
                    <textarea name="value" class="form-control" id="exampleFormControlTextarea1" rows="3">$hashref->{content}</textarea>
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
        </div>
    </body>
</html>
EOF
exit;
据我所知,使用CGI的行上的'-utf8'没有效果

当使用mysql_enable_utf8=0时,程序运行良好,但不区分重音的搜索失败。Umlauts和表情符号在浏览器中正确显示

输出:

现在是所有好人来帮助他们国家的时候了 表情符号->字符集utf8mb4

不区分重音->任何排序规则utf8mb4\u…\u ci

由于您使用的是相对过时的5.5,因此可能会遇到767问题。看

如果您有问号或Mojibake,例如dÃ∗g代表dög,请参阅

我关于Perl的笔记:

use utf8;
use open ':std', ':encoding(UTF-8)';
my $dbh = DBI->connect("dbi:mysql:".$dsn, $user, $password, {
   PrintError => 0,
   RaiseError => 1,
   mysql_enable_utf8 => 1,  # Switch to UTF-8 for communication and decode.
});
# or {mysql_enable_utf8mb4 => 1} if using utf8mb4
我没有任何关于使用CGI的说明。

表情符号->字符集utf8mb4

不区分重音->任何排序规则utf8mb4\u…\u ci

由于您使用的是相对过时的5.5,因此可能会遇到767问题。看

如果您有问号或Mojibake,例如dÃ∗g代表dög,请参阅

我关于Perl的笔记:

use utf8;
use open ':std', ':encoding(UTF-8)';
my $dbh = DBI->connect("dbi:mysql:".$dsn, $user, $password, {
   PrintError => 0,
   RaiseError => 1,
   mysql_enable_utf8 => 1,  # Switch to UTF-8 for communication and decode.
});
# or {mysql_enable_utf8mb4 => 1} if using utf8mb4

我没有任何关于使用CGI的说明。

这是我在CentOS 7上使用的解决方案:

通过yum安装mysql-devel,因为mysql\u config不在我的系统上 通过yum将Perl-DBD::mysql从4.023升级到4.050 在DBI->connect中使用mysql\u enable\u utf8mb4选项 添加binmodeddout,:编码UTF-8;在Perl脚本之上 现在输出符合要求:


现在是所有好人来帮助他们国家的时候了 这是我在CentOS 7上使用的解决方案:

通过yum安装mysql-devel,因为mysql\u config不在我的系统上 通过yum将Perl-DBD::mysql从4.023升级到4.050 在DBI->connect中使用mysql\u enable\u utf8mb4选项 添加binmodeddout,:编码UTF-8;在Perl脚本之上 现在输出符合要求:


现在是所有好人来帮助他们国家的时候了。看看这些文档,我想你想要的是mysql\u enable\u utf8mb4而不是mysql\u enable\u utf8?您应该告诉Perl,标准输出应该通过use open':std',':encodingUTF-8'使用utf-8;或者类似的,如果你正在写Unicode文本,这个表情符号的编码是4字节长的,所以肯定需要mysql\u enable\u utf8mb4。这是唯一的问题吗?@Shawn你的代码没有帮助,但是binmodesdout,:encodingUTF-8;做使用mysql_enable_utf8=>1,我的测试的操作得到了改进:UMLAUT被正确保存和显示,我对狗的测试搜索工作正常。然而,表情符号现在保存为四个问号。mysql_enable_utf8mb4设置似乎在我的环境中不可用。它绝对没有效果。@ikegami通过测试,我已经确定我的环境不支持mysql\u enable\u utf8mb4。不知道该怎么做。你的代码没有帮助,嗯,使用open':std',':encodingUTF-8';不包括:编码UTF-8;看看这些文档,我想你想要的是mysql\u enable\u utf8mb4而不是mysql\u enable\u utf8?您应该告诉Perl,标准输出应该通过use open':std',':encodingUTF-8'使用utf-8;或者类似的,如果你正在写Unicode文本,这个表情符号的编码是4字节长的,所以肯定需要mysql\u enable\u utf8mb4。这是唯一的问题吗?@Shawn你的代码没有帮助,但是binmodesdout,:encodingUTF-8;做使用mysql_enable_utf8=>1,我的测试的操作得到了改进:UMLAUT被正确保存和显示,我对狗的测试搜索工作正常。然而,表情符号现在保存为四个问号。mysql_enable_utf8mb4设置似乎在我的环境中不可用。它绝对没有效果。@ikegami通过测试,我已经确定我的环境不支持mysql\u enable\u utf8mb4。不知道该怎么做。你的代码没有帮助,嗯,使用open':std',':encodingUTF-8';不包括:编码UTF-8;通过测试,我确定我的堆栈不支持mysql\u enable\u utf8mb4。CentOS 7上的百胜表示perl-DBI-1.627-4.el7.x86_64和perl-DBD-MySQL-4.023-6.el7.x86_64已经安装,并且是最新版本。不知道该怎么做,我只是在看。我想我的问题是yum存储库是4.023,我需要DBD::mysql 4.050来支持mysql\u enable\u utf8mb4。我想我应该卸载存储库,然后手动安装新版本。这太旧了。我有一个$VERSION='4.033'的文件;2013年版权所有。@TimothyB.-您可能可以绕过yum从cpan安装。通过测试,我确定我的堆栈不支持mysql\u enable\u utf8mb4。CentOS 7上的百胜表示perl-DBI-1.627-4.el7.x86_64和perl-DBD-MySQL-4.023-6.el7.x86_64
已安装且为最新版本。不知道该怎么做,我只是在看。我想我的问题是yum存储库是4.023,我需要DBD::mysql 4.050来支持mysql\u enable\u utf8mb4。我想我应该卸载存储库,然后手动安装新版本。这太旧了。我有一个$VERSION='4.033'的文件;2013年版权所有。@TimothyB.-您可能可以绕过yum从cpan安装。