简体   繁体   English

PHP UTF-8编码MYSQL插入?

[英]PHP UTF-8 Encoding MYSQL Inserts?

I have a very strange behaviour that I'm experiencing. 我正在经历一种非常奇怪的行为。 When I run insert into with an UTF8 Character (specifically the 'ő' or 'ű' hungarian characters) it inserts a ? 当我使用UTF8字符(特别是'ő'或'ű'匈牙利字符)运行insert into时,它会插入一个? instead of the character. 而不是角色。 However if I echo it out right before passing into the query it shows the right characters. 但是,如果我在进入查询之前立即将其回显,则会显示正确的字符。

What I have done: 我做了什么:

  • I have set every possible collation, charset in the mysql tables and databases to UTF8. 我已经将mysql表和数据库中所有可能的排序规则,字符集设置为UTF8。
  • I have called mysql_query("SET NAMES 'UTF8'"); 我已经叫mysql_query("SET NAMES 'UTF8'");
  • I have called mysql_query("SET CHARACTER SET UTF8"); 我已经叫mysql_query("SET CHARACTER SET UTF8");
  • I have set website encoding with: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 我已将网站编码设置为: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

What works as intended: 预期的工作方式:

  • PHPMyAdmin 的phpMyAdmin
  • Echoing Special Characters from tables 从表中回显特殊字符

It's working on a localhost server. 它正在本地主机服务器上工作。 The non local server's default encoding is ISO-8859-1 which I can't change. 非本地服务器的默认编码为ISO-8859-1,我无法更改。

if you 如果你

1 - set your html page's lang encoding to utf-8 which includes your forms 1-将您的html页面的lang编码设置为utf-8 ,其中包括您的表单
2 - only use your forms to enter input into your related MySQL db tables 2-仅使用表单在相关的MySQL数据库表中输入输入
3 - set all collations to utf8_unicode_ci in your MySQL (tables and rows collations) 3-在您的MySQL中将所有归类设置为utf8_unicode_ci (表和行归类)
4 - if you have premission you can also setyour MySQL DB collation as utf8_unicode_ci 4-如果您有权限,也可以将MySQL DB排序规则设置为utf8_unicode_ci

then you won't see entities in your mySQL records also 那么您也不会在mySQL记录中看到实体

This is my solution I use and have no trouble with my mother language which also has lots of unicode characters. 这是我使用的解决方案,我的母语也没问题,因为母语也有很多unicode字符。

Below I introduce you my db connection php code & recommend (by using prepared statements; please check http://php.net/manual/en/mysqli.quickstart.prepared-statements.php ) 下面我向您介绍我的数据库连接php代码并推荐(通过使用准备好的语句;请检查http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

//mysql bağlantısı
global $db_baglanti;
$db_baglanti = new mysqli(vt_host, vt_user, vt_password, vt_name);
if ($db_baglanti->connect_errno) 
{
    echo "MySQL bağlantısı kurulamadı: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$db_baglanti->set_charset("utf8")) 
{
    printf("utf8 karakter setinin yüklenmesinde problem oluştu: %s\n", $db_baglanti->error);
} 
else 
{
    $db_baglanti->set_charset("utf8");
}

I think main point for you is number 2 in list above. 我认为对您来说,重点是上面列表中的2号。

Also If you already have some document, edit your document's meta tag for charset declaration and use notepad++ encoding>convert to UTF-8 without BOM , save your document, safely go on with your UTF-8 characters from now on. 另外,如果您已经有了一些文档,请编辑文档的meta标签以进行字符集声明,并使用notepad ++ encoding>convert to UTF-8 without BOM ,保存文档,从现在开始安全地使用UTF-8字符。 (this is a solution for possible HTML side issues related to BOM ) (这是解决与BOM相关的HTML方面问题的解决方案)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM