INSERT 查询中的西里尔字母符号
$dblocation = "localhost";
$dbname = "xx";
$dbuser = "xx";
$dbpasswd = "xx";
$dbcnx = @mysql_connect($dblocation,$dbuser,$dbpasswd);
mysql_select_db($dbname, $dbcnx);
mysql_query("SET NAMES utf8");
mysql_query("SET COLLATION_CONNECTION=utf8_bin");
$name = mysql_real_escape_string($name);
$about = mysql_real_escape_string($about);
mysql_query("INSERT INTO votes
(name, about, active) VALUES('".$name."', '".$about."', 1 ) ")
or die(mysql_error());
当 $name 和 $about 不是西里尔字母时,它工作得很好。但是,当它是西里尔文时,脚本只会添加一行带有空白名称和有关字段的行。做什么?
DB 是 UTF-8,使用带有西里尔字母符号的 phpMyAdmin 手动添加行效果很好,PHP 脚本是 UTF-8,一切都是 UTF-8。
$dblocation = "localhost";
$dbname = "xx";
$dbuser = "xx";
$dbpasswd = "xx";
$dbcnx = @mysql_connect($dblocation,$dbuser,$dbpasswd);
mysql_select_db($dbname, $dbcnx);
mysql_query("SET NAMES utf8");
mysql_query("SET COLLATION_CONNECTION=utf8_bin");
$name = mysql_real_escape_string($name);
$about = mysql_real_escape_string($about);
mysql_query("INSERT INTO votes
(name, about, active) VALUES('".$name."', '".$about."', 1 ) ")
or die(mysql_error());
It works perfectly fine when $name and $about is not cyrillic. But when it's cyrillic - script just adds a row with blank name and about fields. What do?
DB is UTF-8, manual adding rows with phpMyAdmin with cyrillic symbols works perfect, PHP-script is UTF-8, everything's UTF-8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该有:
1.
header ("Content-Type: text/html; charset=utf-8");
或2.mysql_query("设置名称 'utf8'");
3.将文件编码设置为utf-8 without BOM
You should have:
1.
header ("Content-Type: text/html; charset=utf-8");
or<meta http-equiv="content-type" content="text/html; charset=utf-8" />
2.mysql_query("SET NAMES 'utf8'");
3.Set your file's encoding to utf-8 without BOM
使用
mysql_set_charset(...')
指定连接应使用的字符集。请参阅 http://php.net/manual/en/function.mysql- set-charset.php。Specify character set which should be used by the connection with
mysql_set_charset(...')
. See http://php.net/manual/en/function.mysql-set-charset.php.我已经处理过很多西里尔字母了,这确实很棘手。到目前为止,根据我的经验,我没有使用任何 COLLATION_CONNECTION。我所做的就是像您一样将所有可能的文件放入 utf8 中。然后,我将数据库中的字段放入“utf8_unicode_ci”中,并且仅使用 SET NAMES 参数来设置连接编码。这应该足够了。请注意,集合名称可能很棘手,因为 extra 中只有一个空格,并且它不起作用。我使用:
我希望我能帮上忙。
I've dealt a lot with the cyrillic and it is tricky indeed. So far in my experience I did not use any COLLATION_CONNECTION. All I do is put, as you did, all possible files in utf8. Then the fields in the DB I put in "utf8_unicode_ci" and I only use the SET NAMES params to set the connection encoding. This should be enough. Mind that the Set names might be tricky, as only a single space in extra and it will not work. I use:
I hope I managed to help.