mySQL and PHP encodings

眉间皱痕 提交于 2019-12-06 11:48:45

问题


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


回答1:


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() function:

mb_internal_encoding("UTF-8");

By default PHP uses ISO-8859-1.




回答2:


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!




回答3:


try regexp with the desired regular expression like

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




回答4:


Have you tried SET NAMES?

mysql_query("SET NAMES utf8"); 


来源:https://stackoverflow.com/questions/5420787/mysql-and-php-encodings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!