php - 为什么非英文文本没有被插入到 mysql 中?

标签 php mysql encoding mysqli

我正在尝试将标题插入 mysql 数据库字段类型 char(255) 中,如下所示

$text = 'Лига Справедливости Кризис двух Миров (Justice League Crisis on Two Earths)';

$stmt = mysqli_prepare($con, "INSERT INTO temp (title) VALUES (?)");
mysqli_stmt_bind_param($stmt, 's', $text);
mysqli_stmt_execute($stmt);
printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
mysqli_stmt_close($stmt);

但是当我在数据库中看到插入的值时,它显示

这是为什么呢? 我该如何修复它?

<小时/>

更多信息

mysql> status
--------------
mysql  Ver 14.14 Distrib 5.5.39, for Linux (x86_64) using readline 5.1

Connection id:          457
Current database:
Current user:           root@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.5.39 MySQL Community Server (GPL)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    utf8
Db     characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /var/lib/mysql/mysql.sock
Uptime:                 2 days 13 hours 46 min 13 sec

Threads: 1  Questions: 1419373  Slow queries: 0  Opens: 1001  Flush tables: 1  Open tables: 45  Queries per second avg: 6.382
--------------
<小时/>

我试过了

1)

if (!mysqli_set_charset($con, "utf8")) {
    printf("Error loading character set utf8: %s\n", mysqli_error($con));
    die();
}

2)

$text = @iconv('UTF-8', 'UTF-8//IGNORE', $text);

最佳答案

尝试在 php 中使用 urlencode() 函数。使用下面的代码

$text = urlencode('Лига Справедливости Кризис двух Миров (Justice League Crisis on Two Earths)');

$stmt = mysqli_prepare($con, "INSERT INTO temp (title) VALUES (?)");
mysqli_stmt_bind_param($stmt, 's', $text);
mysqli_stmt_execute($stmt);
printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
mysqli_stmt_close($stmt);

获取数据时只需使用 urldecode() 函数解码值

<?php
 $text = urlencode('Лига Справедливости Кризис двух Миров (Justice League Crisis on Two Earths)');
echo $text."<br>"; // will print the encoded text
echo urldecode($test); // Will print the decoded text

希望这对你有帮助

关于php - 为什么非英文文本没有被插入到 mysql 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25458817/

相关文章:

MySQL SELECT 其中 bool 字段为 NULL 或 false

ios - 将希伯来语文本保存到 NSUserDefaults 返回奇怪的编码

php - 检查用户名是否存在于Android代码中

php - Zend框架: how to reset mysite. com/admin/auth密码在mysql中使用phpmyadmin

python - 如何将 Python 连接到 MySQL 数据库...?

php - 如何使用 MySQL 将学生分配给最喜欢的室友

video - FFMPEG 水印视频无需重新编码或质量下降

linux - 在 Linux 上搜索文件中的错误编码字符

php - 从 Laravel 中的 Mysql 数据透视表获取列并返回一个 JSON 对象

php - 如何使用 PHP 登录 Youtube?