php - 无法将俄语文本插入mysql数据库

标签 php mysql unicode utf-8

当我尝试将俄语文本插入 MySQL 数据库时,它会像这样插入: ????????? ?? ????????
Рісѓрїр°ріс‹рїр°с‹рір°рї

所以,我有两个页面:registration.php 和 addUser.php。在他们每个人中

 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

数据库由11张表组成,每张表的排序规则:utf8_general_ci,类型:MyISAM。每个表中的每个字段都有 Collat​​ion:utf8_general_ci。

当我直接在 phpMyAdmin 中写入数据库,然后将此数据显示到网页时。英语和俄语 - 都可以。

但是当我在 registration.php 上用个人数据填满我的表格,然后转到 addUser.php - 所有俄语字符都像我上面写的那样显示 - 在页面和数据库中也是如此。

    function AddNewUser($Name, $Surname, $FatherName, $Email, $Password, $Phone, $DegreeID, $RankID, 
$Organization, $Department, $Country, $City, $Address, $Job)
{
        //fetch data from database for dropdown lists
        //connect to db or die)
    $db = mysql_connect($GLOBALS["gl_kussdbName"], $GLOBALS["gl_kussUserName"], $GLOBALS["gl_kussPassword"] ) or die ("Unable to connect");

    //to prevenr ????? symbols in unicode - utf-8 coding
    mysql_query("SET NAMES 'UTF8'");

    //select database
    mysql_select_db($GLOBALS["gl_kussDatabase"], $db);
    $sql = "INSERT INTO UserDetails (
UserFirstName,
UserLastName,
UserFatherName,
UserEmail,
UserPassword,
UserPhone,
UserAcadDegreeID,
UserAcadRankID,
UserOrganization,
UserDepartment,
UserCountry,
UserCity,
UserAddress,
UserPosition) 
VALUES(
'".$Name."',
'".$Surname."',
'".$FatherName."',
'".$Email."',
'".$Password."',
'".$Phone."',
'".$DegreeID."',
'".$RankID."',
'".$Organization."',
'".$Department."',
'".$Country."',
'".$City."',
'".$Address."',
'".$Job."'
);";
    //execute SQL-query
    $result = mysql_query($sql, $db);
    if (!$result) 
    {
        die('Invalid query: ' . mysql_error());
    }
    //close database  = very inportant
    mysql_close($db);

}
?>

phpMyAdmin中也有这样的信息:

auto increment increment    1
auto increment offset   1
autocommit  ON
automatic sp privileges ON
back log    50
basedir \usr\local\mysql-5.1\
big tables  OFF
binlog cache size   32,768
binlog format   STATEMENT
bulk insert buffer size 8,388,608
character set client    utf8
(Global value)  cp1251
character set connection    utf8
(Global value)  cp1251
character set database  cp1251
character set filesystem    binary
character set results   utf8
(Global value)  cp1251
character set server    cp1251
character set system    utf8
character sets dir  \usr\local\mysql-5.1\share\charsets\
collation connection    utf8_general_ci
(Global value)  cp1251_general_ci
collation database  cp1251_general_ci
collation server    cp1251_general_ci
completion type 0
concurrent insert   1

所以我需要从数据库中正确显示、保存和选择俄语文本。谢谢! 连接超时 10 数据目录\usr\local\mysql-5.1\data\

最佳答案

尝试在连接到数据库后调用 mysql_set_charset('utf8');。我认为这类似于执行 SET NAMES 查询,但由于 PHP 手册说建议在 SET NAMES 查询上使用该函数,所以我会尝试一下。

此外,当您显示内容时,您可以尝试 echo htmlentities($string, ENT_COMPAT, 'UTF-8');

关于php - 无法将俄语文本插入mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4404768/

相关文章:

php - 如何在 PHP 中将字符串转换为符号

mysql - 如何将 2 个查询作为单个查询同时运行

mysql - 如何在mysqld_safe运行场景中设置mysql的密码?

php - 排序 html php mysql 表的问题

php - Codeigniter:MP4 视频上传不工作

php - Google Cloud Vision - 解析期间发生 PHP 错误

javascript - Node.js Sequelize ManyToMany 关系产生不正确的 SQL

python - 使用 Cairo 渲染器的 Matplotlib Unicode 轴标签

java - 波兰语字母在 MZ 320 CPCL 中不起作用

haskell - 来自 Char 的 Unicode 名称