为什么非英语文本在mysql中没有被INSERTED?

时间:2014-08-23 05:38:27

标签: php mysql encoding mysqli

我正在尝试INSERT 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);

1 个答案:

答案 0 :(得分:0)

尝试在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

希望这有助于你