gpt4 book ai didi

mysql - 具有重音不敏感排序规则的 Web 应用程序中的 Unicode 支持(包括表情符号)

转载 作者:行者123 更新时间:2023-11-29 04:32:23 24 4
gpt4 key购买 nike

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

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

数据库小部件的转储:

DROP TABLE IF EXISTS `experiment`;
CREATE TABLE `experiment` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(40) CHARACTER SET utf8mb4 DEFAULT NULL,
`content` text CHARACTER SET utf8mb4,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

LOCK TABLES `experiment` WRITE;
INSERT INTO `experiment` VALUES (1,'Record','Now is the time for all good men to come to the aid of their country. 😀\r\nThe quick brown fox jumped over the lazy dög.');
UNLOCK TABLES;

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;

据我所知,use CGI 行中的'-utf8' 没有任何效果。

当使用 mysql_enable_utf8 = 0 时,程序工作得很好 但是不区分重音的搜索失败。变音符号和表情符号在浏览器中正确显示。

输出:

Content = Now is the time for all good men to come to the aid of their country. 😀 The quick brown fox jumped over the lazy dög. String: [fox] found [1] String: [dog] found [0E0]

当使用“mysql_enable_utf8 = 1”时,HTML 输出看起来乱七八糟。

输出:

Content = Now is the time for all good men to come to the aid of their country. 😀 The quick brown fox jumped over the lazy dög. String: [fox] found [1] String: [dog] found [0E0]

我觉得我很接近,但缺少一些重要的东西。

最佳答案

表情符号 --> CHARACTER SET utf8mb4.

不区分重音 --> 任何 COLLATION utf8mb4_..._ci

由于你是比较老式的5.5,可能会遇到“767问题”。参见 http://mysql.rjweb.org/doc.php/limits#767_limit_in_innodb_indexes

如果您有问号或 Mojibake(例如 dögdög),请参阅 Trouble with UTF-8 characters; what I see is not what I stored

我的 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的笔记。)

关于mysql - 具有重音不敏感排序规则的 Web 应用程序中的 Unicode 支持(包括表情符号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com