mySQL和PHP编码 [英] mySQL and PHP encodings
问题描述
HI,我有这个查询的问题
I have a problem with this query
SELECT * FROM table WHERE `name` LIKE '%tést%'
HMTL和SQL表都有utf-8编码,但遗憾的是没有$ code> mysql_set_charset('utf-8')而我不可以使用它。
the HMTL and the SQL table both have utf-8 encoding but unfortunately there is no mysql_set_charset('utf-8')
and I'm NOT able to use it.
我'\\'尝试使用 iconv(UTF-8,ISO-8859-1,$ name)
,但查询只匹配 test
。
I've tried with iconv("UTF-8", "ISO-8859-1", $name)
but the query matches only test
.
我想匹配所有这些: test,tést,tèst,tëst
因为它可以与 mysql_set_charset
一起使用)
I want to match all of these : test, tést, tèst, tëst
(as it would work with mysql_set_charset
)
编辑:
SET NAMES utf8
更为可能...数据库是使用utf8编码,不幸的是内容正在从$ code> mysql_set_charset 或 SET NAMES
。
SET NAMES utf8
is nigher possible ... the database is with utf8 encoding, unfortunately the content is being filled from web without mysql_set_charset
nor SET NAMES
.
目前,如果使用这些功能,结果就会搞砸。
Currently if these functions are used the results are messed up.
version()
5.1.41-3ubuntu12.9
edit2:
当我使用 SET NAMES utf8
它只匹配tést
,它们看起来像<$当我使用 iconv(UTF-8,ISO-8859)时,c $ c> t?st
when I use SET NAMES utf8
it matches only tést
and they look like tést
-1,$ name)它只匹配 test
when I use iconv("UTF-8", "ISO-8859-1", $name)
it matches only test
推荐答案
在查询之前尝试:
mysql_query("SET NAMES 'utf8'", $conn);
* 编辑 *
*edits*
根据MySQL版本,您可能还需要使用:
Apprently depending on MySQL version you might also be required to use:
mysql_query("SET CHARACTER SET utf8", $conn);
最后要注意的是,数据库需要使用UTF-8字符集,以确保:
One final note, the database needs to be using the UTF-8 character set, to ensure this:
ALTER <database_name> DEFAULT CHARACTER SET utf8;
* 更多编辑 *
*More edits*
阅读完编辑后,我认为这是HTML / PHP编码的问题。在提交字符的页面上确保您正确设置标题:
After reading your edits I think that this is a issue with your HTML/PHP encoding. On the page submitting the characters ensure that you set the headers properly:
header('Content-Type: text/html; charset=UTF-8');
您还应该通过元标记设置:
You should also set this via meta tags:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
然后使用 mb_internal_encoding()
功能:
Then set the multibyte encoding with the mb_internal_encoding()
function:
mb_internal_encoding("UTF-8");
默认情况下,PHP使用ISO-8859-1。
By default PHP uses ISO-8859-1.
这篇关于mySQL和PHP编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!