简体   繁体   English

UTF-8编码问题

[英]UTF-8 encoding problems

I have a site that is storing spanish characters in a MySQL DB. 我有一个站点,该站点在MySQL DB中存储西班牙语字符。 All tables and DB are configured UTF-8 and is identically loaded onto my local WAMP setup and to the hosting account server as much as I can reasonably tell. 所有表和数据库均已配置为UTF-8,并且据我所知,它们已完全加载到我的本地WAMP设置和托管帐户服务器中。

Spanish characters display perfectly locally but on the live server the dreaded question mark symbol is displayed for the accented characters. 西班牙语字符在本地完美显示,但在实时服务器上,重音符号显示了可怕的问号符号。 Only the text generated dynamically from the DB is affected so it would be reasonable to assume that this is somehow related to my queries, connection or DB in some way. 仅影响从数据库动态生成的文本,因此可以合理地假设这与我的查询,连接或数据库以某种方式相关。

<meta charset="utf-8">
 <title>Untitled Document</title>
   </head>

   <select multiple class='form-control' name='location[]' id='location'>
      encoding: UTF-8<option value='Alhaur�n de la Torre'>Alhaur�n de la Torre</option>
      encoding: ASCII<option value='Artola'>Artola</option>
      encoding: ASCII<option value='Bel Air'>Bel Air</option>

Using mb_detect_encoding() my dropdown looks as above. 使用mb_detect_encoding(),我的下拉菜单如上所述。

My header contains: 我的标题包含:

 header('Content-type: text/html; charset=utf-8');

Database is utf8_general_ci for tables, server connection collation, server charset is UTF-8 Unicode (utf8) 表的数据库为utf8_general_ci,服务器连接排序规则,服务器字符集为UTF-8 Unicode(utf8)

I have used SET NAMES utf8 in my PDO DB connection. 我在PDO DB连接中使用了SET NAMES utf8。

This is driving me crazy, why does it work locally but not on my hosting server. 这让我发疯,为什么它在本地却不能在我的托管服务器上工作。 After much searching for an answer I am hoping for some guidance here. 经过大量寻找答案后,我希望在这里提供一些指导。

Thanks in advance 提前致谢

SOLVED: 解决了:

In my connection script I changed this: 在我的连接脚本中,我对此进行了更改:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass);
 $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');

To this: 对此:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')); 

and all works as it should. 一切都应有的工作。

Thanks for looking 谢谢看

SOLVED: 解决了:

In my connection script I changed this: 在我的连接脚本中,我对此进行了更改:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass);
 $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');

To this: 对此:

 $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;", $dbUser, $dbPass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

and all works as it should. 一切都应有的工作。

Thanks for looking 谢谢看

$dbh = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=UTF8", $dbUser, $dbPass);

将字符集添加到pdo连接中!

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

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