问题描述
我试图输出存储在 MySQL 数据库中的产品信息,但它写出了一些奇怪的字符,比如里面有一个问号的菱形.
I'm trying to output product information stored in a MySQL database, but it's writing out some strange characters, like a diamond with a question mark inside of it.
我认为这可能是编码/UTF8 问题,但我已经指定了我想要的编码:
I think it may be an encoding/UTF8 issue, but I've specified the encoding I want:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
这是对的吗?我应该检查什么?
Is this right? What should I check for?
推荐答案
如果只有来自数据库的数据中有奇怪的字符,请使用以下方法确保 MySQL 连接也是 UTF8:
If only the data that's coming from database has strange characters in it, be sure that the MySQL connection is also in UTF8 by using:
mysql_query("SET NAMES UTF8");
在任何其他查询之前.否则,如果字符也出现在手写"文件中,请确保在编辑器中将文件保存为 UTF-8.您也可以尝试通过 PHP 设置字符集标头:
before any other queries. Otherwise, if the characters appear also in 'handwritten' files, make sure that the files are saved as UTF-8 in your editor. You can also try setting the charset header through PHP:
header('Content-type: text/html; charset=UTF-8');
还要确保您查询的表中的所有字段都设置为某种 UTF-8 变体,例如 utf8_general_ci
.
Also make sure that all fields in the tables you are querying are set as some UTF-8 variant, for example utf8_general_ci
.
这篇关于PHP MySQL 数据库奇怪字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!