mySQL 和 PHP 编码

发布于 2024-10-25 19:19:44 字数 896 浏览 8 评论 0原文

您好,我对此查询有疑问,

SELECT * FROM table WHERE `name` LIKE '%tést%'

HMTL 和 SQL 表都有 utf-8 编码,但不幸的是没有 mysql_set_charset('utf-8') 并且我不< /strong> 能够使用它。

我尝试过使用 iconv("UTF-8", "ISO-8859-1", $name) 但查询仅匹配 test

我想匹配所有这些: test, tést, tèst, tëst (因为它可以与 mysql_set_charset 一起使用)

编辑:

SET NAMES utf8 几乎是可能的......数据库使用utf8编码,不幸的是内容是从网络填充的,没有mysql_set_charset也没有SET NAMES< /代码>。

目前,如果使用这些函数,结果会很混乱。

version() 5.1.41-3ubuntu12.9

edit2:

当我使用 SET NAMES utf8 时,它仅匹配 < ,它们看起来像 tést

code>tést当我使用 iconv("UTF-8", "ISO-8859-1", $name) 时 它仅匹配 test

HI, I have a problem with this query

SELECT * FROM table WHERE `name` LIKE '%tést%'

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.

I've tried with iconv("UTF-8", "ISO-8859-1", $name) but the query matches only test.

I want to match all of these : test, tést, tèst, tëst (as it would work with mysql_set_charset )

edit:

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:

when I use SET NAMES utf8 it matches only tést and they look like tést

when I use iconv("UTF-8", "ISO-8859-1", $name) it matches only test

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ˉ厌 2024-11-01 19:19:44

在查询之前尝试:

mysql_query("SET NAMES 'utf8'", $conn);

编辑

显然,根据 MySQL 版本,您可能还需要使用:

mysql_query("SET CHARACTER SET utf8", $conn);

最后一点,数据库需要使用 UTF-8 字符集,确保这一点:

ALTER <database_name> DEFAULT CHARACTER SET utf8;

更多编辑

阅读您的编辑后,我认为这是您的 HTML/PHP 编码的问题。在提交字符的页面上,确保正确设置标题:

header('Content-Type: text/html; charset=UTF-8');

您还应该通过元标记进行设置:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

然后使用 [mb_internal_encoding()][1] 函数设置多字节编码:

mb_internal_encoding("UTF-8");

默认 PHP使用 ISO-8859-1。
[1]: https://www.php.net/mb_internal_encoding

Before the query try:

mysql_query("SET NAMES 'utf8'", $conn);

edits

Apprently depending on MySQL version you might also be required to use:

mysql_query("SET CHARACTER SET utf8", $conn);

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

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">

Then set the multibyte encoding with the [mb_internal_encoding()][1] function:

mb_internal_encoding("UTF-8");

By default PHP uses ISO-8859-1.
[1]: https://www.php.net/mb_internal_encoding

对你再特殊 2024-11-01 19:19:44

我刚刚进行了一次快速测试,MySQL 的工作方式正如您所期望的那样。

也许这是你连接数据库的方式,你可以尝试执行 SET NAMES 'utf8';在执行查询之前看看是否有帮助!

另外,请注意 mysql_set_charset 应采用 utf8,不带破折号!请参阅PHP 示例来确定!

干杯!

I have just performed a quick test, and MySQL works as you expect it to work.

Perhaps it's the way you connect to the database, you could try executing SET NAMES 'utf8'; before performing the queries and see if that helps!

Also, please note that mysql_set_charset should take utf8, with no dash! See an example from PHP to be sure!

Cheers!

许久 2024-11-01 19:19:44

尝试使用所需的正则表达式进行 regexp,例如

SELECT * FROM table WHERE name REGEXP #REGULAR EXP HERE#

try regexp with the desired regular expression like

SELECT * FROM table WHERE name REGEXP #REGULAR EXP HERE#

少年亿悲伤 2024-11-01 19:19:44

您尝试过设置名称吗?

mysql_query("SET NAMES utf8"); 

Have you tried SET NAMES?

mysql_query("SET NAMES utf8"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文