Re: Re: 0x9f54

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Man-wai Chang wrote:
> how do you do string comparison in utf-8? say the user entered a chinese
> string the browser and you need to search for the string in the MySQL
> table using PHP.
>   
    How?  Of course in the usual PHP way!  (Be warned that we're getting
more and more off-topic.  This is a PHP forum!)

    You have to make sure that the following conditions be true:
1. Your database tables are using utf8, at least the column in
question.  I've noticed that while I use VCHAR(n) as the data type, you
simply use CHAR(n) (seen in your test.php.txt file).  I'm not MySQL
expert, so I don't know the difference between them.

    Concretely, you're creating a table like this:

CREATE TABLE `t1` (
  `name` varchar(45) character set utf8 NOT NULL default '',
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

2. Everywhere in PHP, you're using UTF-8 to assure a consistency.  Avoid
making conversion to and from Big5, you'll get yourself trapped sooner
or later.

3. In HTML, make sure that you've specified UTF-8 in HTTP header as well
as in HTML header.

4. Getting data: the encoding in a URI is implicit, and some stupid/old
browser doesn't know how to handle correctly utf-8.  So, I'd suggest you
to use POST instead of GET to avoid problem in URI encoding.

    Once all these conditions are unified, you should be able to do
something like:
SELECT * FROM t1 where name like '%明%';
to search for all strings containing the character 明.  As a side note,
this query works perfectly even if your strings contains other non
Chinese characters.  My table t1 contains "Gillian Joe", "小明", "陳大明
", "François 阿偉" and "Emily 明美".

    You have to make sure that you're communicating this character in
UTF-8 instead of Big5 to MySQL.  Normally, if you have specified the
charset in PHP, it's automatic.

    Good luck.



----------
* Zoner PhotoStudio 8 - Your Photos perfect, shared, organised! www.zoner.com/zps
  You can download your free version.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux