导入的数据库转储从 latin1 数据库到 utf8 数据库
当我从 mysql v4.0.21 对数据库进行 mysql 转储并将其导入到新服务器 mysql v5.0.45
旧服务器上是 latin1,新服务器上是 utf8,所以我在 mysql 转储上运行了这个:iconv -f latin1 -t UTF-8 Quickwebcms_2010-03-01.sql
运行成功,然后我将其导入到新服务器上。
现在它显示问号 (?
) 标记(例如:College?s
)和 (例如:
College's
)当它打印出我的 PHP 应用程序中的一些数据时。
我导出了这些字符显示的表,并在 textmate 中查找并替换了所有内容,然后将其导入回新数据库,并将某些字段上传为空,因此查找和替换可能会在过程中弄乱某些内容。我将表 csv 保存为 utf8 no bom,并且只是 utf8,它仍然执行相同的操作。
任何关于为什么会发生这种情况的帮助都会受到赞赏。
I used iconv
to convert from latin1 to utf8 when I did an mysql dump of a database from mysql v4.0.21, and imported it onto a new server mysql v5.0.45
It was latin1 on the old server, it’s utf8 on the new server, so I ran this on the mysql dump: iconv −f latin1 −t UTF−8 quickwebcms_2010-03-01.sql
It ran successful, then I imported it onto the new server.
Now it displays question (?
) marks (example: College?s
) and Â
(example: CollegeÂ’s
) when it prints out some of the data in my PHP application.
I exported the table these characters show up in and did a find and replace all within textmate, then imported it back into the new database and it uploads some of the fields as null, so the find and replace may of messed up something in the process. I saved the table csv as utf8 no bom, and just utf8 and it still does the same thing.
Any help as to why this might be happening is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果表的内容全部正常(并且采用 UTF-8),并且 Web 应用程序中仍然存在“错误”字符,请确保您的 MySQL 连接在 PHP 脚本中使用 UTF-8 字符集。即使您的数据库和表采用 UTF-8 格式,MySQL 默认情况下也使用 latin1 连接(至少在我的共享服务器配置中)。所以你必须告诉MySQL以UTF-8发送内容。否则,它会即时将其转换为 latin1,从而在 UTF-8 网页中产生“坏”字符。
如果可用,请使用 mysql_set_charset 否则您可以使用SQL 查询(如果可用,请始终使用 mysql_set_charset):
还要确保您的 (X)HTML 标记也使用 UTF-8:
If the content of your tables are all OK (and in UTF-8) and you sill have "bad" characters in your Web application, make sure your MySQL connection is using the UTF-8 charset in your PHP script. Even if your databases and tables are in UTF-8, MySQL uses latin1 connections by default (at least in my shared server config). So you have to tell MySQL to send content in UTF-8. Otherwise it will convert it on the fly to latin1 producing "bad" characters in UTF-8 webpages.
Use mysql_set_charset if available otherwise you can set it with a SQL query (always use mysql_set_charset if available):
Also make sure your (X)HTML markup uses UTF-8 too:
IIRC,
mysqldump
默认生成 UTF-8 输出,无论数据库的编码是什么。此用户评论在 mySQL 手册 似乎证实了这一点:尝试跳过 iconv 步骤,可能会立即起作用。
IIRC,
mysqldump
produces UTF-8 output by default, no matter what the database's encoding is. This user comment in the mySQL manual seems to confirm it:Try skipping the iconv step, might work straight away.
您可能最好将数据作为 latin1 加载到新服务器上,然后在每个表上使用适当的 ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 DEFAULT COLLATION utf8_unicode_ci (或者使用某种脚本来执行给你的)。
或者你可以先转换,然后转储。
You may be better off loading the data onto the new server as latin1, then using the appropriate
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 DEFAULT COLLATION utf8_unicode_ci
on each table (or use a script of some sort to do it for you).Or you could convert first, then dump.