我正在尝试将标题插入 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/