简体   繁体   English

查询中的问题(PHP MYSQL UTF-8)

[英]Problem in query (PHP MYSQL UTF-8 )

After reading the many tips here and elsewhere, I think I have finally made my GET query, PHP and MYSQL all talk utf8. 在阅读了这里和其他地方的许多提示后,我想我终于完成了我的GET查询,PHP和MYSQL都在谈论utf8。 However, I cannot get a simple query to work where the string I'm matching contains non-ascii characters. 但是,我无法得到一个简单的查询来处理我匹配的字符串包含非ascii字符。 My (simplified) code is: 我的(简化)代码是:

$link = mysql_connect("h", "u", "p") or die(mysql_error());
mysql_select_db("db", $link) or die(mysql_error($link));
mysql_set_charset("utf8", $link); //connection charset
$name = mysql_real_escape_string($_GET['first'], $link);
$query = sprintf("SELECT name, date FROM guestbook WHERE name='%s'", $name);
$result = mysql_query($query, $link) or die(mysql_error()); 
if (mysql_num_rows($result) == 0) {
...
}

If 'first' is ascii, then the query works, if it contains accented characters it does not. 如果'first'是ascii,则查询有效,如果它包含重音字符则不然。 When I print out the db (in an table generated by php) it looks just fine, as does viewing in it phpadmin. 当我打印出db(在php生成的表中)时,它看起来很好,就像在phpadmin中查看一样。

What am I doing wrong? 我究竟做错了什么? Do I need to tell the '=' operator to use utf8? 我需要告诉'='运算符使用utf8吗? Why? 为什么?

Thanks 谢谢

abo ABO

What's the character set of the table itself ? 表本身的字符集是什么?

Also, try running the following query just after connection: 此外,尝试在连接后运行以下查询:

SET NAMES UTF8;

Have you tried encoding the name as UTF-8 before the query? 您是否尝试在查询之前将名称编码为UTF-8?

$query = sprintf("SELECT name, date FROM guestbook WHERE name='%s'", utf8_encode($name));

If that doesn't do the trick, try utf8_decode . 如果这不起作用,请尝试utf8_decode It sounds weird but I had weird cases where it did the trick. 这听起来很奇怪,但我有奇怪的情况,它做了伎俩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM