mysql 和编码
我将我的 php 应用程序移至新服务器。我使用mysql5数据库。当我更新或插入某些内容到数据库时,每个 "
和 -
符号都会更改为 ?
。我使用 SET NAMES UTF8< /code> 和
SET CHARACTER SET
但它不起作用有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在选择以及更新或插入时,每个页面都应使用
SET NAMES UTF8
。实际上,每次连接数据库时都必须使用此查询。只需将其添加到连接代码即可。
SET NAMES UTF8
should be used on every page, when selecting as well as when updating or inserting.actually this query must be used every time you connect to the database. just add it to connect code.
您始终需要 UTF-8 才能使智能引号和破折号 (“”—) 以及其他非 ASCII 字符可靠地工作:
(1) 确保浏览器向您发送编码为 UTF-8 的字符。通过将包含表单的页面声明为 UTF-8 来完成此操作:(
忽略
(2) PHP 处理原始数据bytes 并且不关心它们采用什么编码,但数据库确实关心,因此您必须告诉它来自 PHP 的字节采用什么编码。这就是
SET NAMES
正在做的事情,虽然 mysql_set_charset 可能更可取。(3) 一旦正确的字符到达数据库,就需要将它们存储在 Unicode 编码中,以确保所有字符都能容纳。每列可以有不同的编码,但您可以在
CREATE table
时使用DEFAULT CHARACTER SET utf8
以使其中的所有文本列都使用UTF-8。如果您愿意,还可以将数据库或整个服务器的默认字符集设置为 utf8。如果您已经
CREATE
创建了表并且它们是非UTF-8排序规则,则必须重新创建或更改 表。您可以使用SHOW FULL COLUMNS FROM sometable;
检查当前排序规则。(4) 确保使用
htmlspecialchars()
对 PHP 输出的文本进行 HTML 编码,而不是htmlentities()
,默认情况下,这会弄乱非 ASCII 字符。[作为 (2) 和 (3) 的替代方案,您可以仅对连接和表存储使用默认的 Latin-1 编码,但仍将 UTF-8 字节放入其中。这种方法的缺点是,对于查看数据库的其他工具来说,它看起来是错误的,并且小写/大写字符不会以预期的不区分大小写的方式相互比较。]
You need UTF-8 all the way through to make smart quotes and dashes (“”—) and other non-ASCII characters work reliably:
(1) Ensure that the browser sends you characters encoded to UTF-8. Do this by declaring the page that includes the form to be UTF-8:
(Ignore
<form accept-encoding>
, which doesn't work right in IE.)(2) PHP deals with raw bytes and doesn't care what encoding they're in, but the database does care, so you have to tell it what encoding the bytes from PHP are coming in. This is what
SET NAMES
is doing, though mysql_set_charset may be preferable.(3) Once the proper characters have reached the database, it'll need to store them in a Unicode encoding to make sure all characters can fit. Each column can have a different encoding, but you can use
DEFAULT CHARACTER SET utf8
when youCREATE table
to make all the text columns in it use UTF-8. You can also set the default character set for a database or the whole server toutf8
if you prefer.If you have already
CREATE
d the tables and they a non-UTF-8 collation, you'll have to recreate or alter the tables. You can check the current collation usingSHOW FULL COLUMNS FROM sometable;
.(4) Make sure you HTML-encode text you output from PHP using
htmlspecialchars()
and nothtmlentities()
, which by default will mess up non-ASCII characters.[You can, as an alternative to (2) and (3), just use the default Latin-1 encoding for the connection and the table storage, but put UTF-8 bytes in it nonetheless. The disadvantage of this approach is that it'll look wrong to other tools looking at the database, and lower/upper case characters won't compare against each other in the expected case-insensitive way.]
我的猜测是您正在从某个文本编辑器粘贴,该编辑器将
"
转换为有角度的漂亮引号,并将您的-
转换为 mdash,这导致两者都被表示 。当您将数据库设置为接受 UTF8 字符时,您可能没有将网络服务器/PHP 设置为接受这些字符,但请检查一下 确保您没有使用倾斜的引号或破折号。
My guess is you are pasting from some text editor which is transforming the
"
into an angled pretty quote, and transforming your-
into an mdash, which is causing both to be represented as?
.While you set your database to accept UTF8 characters, you probably did not set your webserver/PHP to accept those characters. Try playing with
mbstring
functions, but check to make sure you arent using the slanted quotes or dashes.