我正在使用PHP和MySQL innodb (PDO),下面是表格结构。
CREATE TABLE `articles` (
`id` int(11) NOT NULL,
`feed_id` mediumint(8) unsigned NOT NULL,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`link` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`pubdate` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL,
`loaddate` date DEFAULT NULL,
`uniqid` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`source` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`,`feed_id`),
KEY `feed_id` (`feed_id`),
KEY `link` (`link`,`uniqid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci以下是我的PHP代码
$DB->sql = "INSERT INTO feed_articles (feed_id,title,link,description,image,video,audio,pubdate,loaddate,uniqid,source) VALUES ";
$DB->sql .= "(".$pfid.",".$DB->quote($RSSarticles[$i]["title"])
.",".$DB->quote($RSSarticles[$i]["link"])
.",".$DB->quote($RSSarticles[$i]["desc"])
.",".(isset($RSSarticles[$i]["image"])?$DB->quote($RSSarticles[$i]["image"]):"''")
.",".(isset($RSSarticles[$i]["video"])?$DB->quote($RSSarticles[$i]["video"]):"''")
.",".(isset($RSSarticles[$i]["audio"])?$DB->quote($RSSarticles[$i]["audio"]):"''")
.",".$DB->quote($RSSarticles[$i]["date"])
.",CURRENT_DATE,"
.$DB->quote($RSSarticles[$i]["id"])
.",".$DB->quote($RSSarticles[$i]["source"])
.") ";
$DB->execute($DB->sql);问题是它没有正确地写入其他语言的字符,它正在进行一些转换并写入数据库。如果我直接在数据库中复制粘贴,那就没问题了。但是使用PHP,我不确定哪里出了问题。
谁知道我应该如何让它在所有语言中工作,比如中文/德文/俄文?
为我执行此操作的每个连接更新。:
function connection($server,$usernamedb,$passworddb,$dbname)
{
//$this->id = @mysql_connect($server, $usernamedb, $passworddb);
$this->id = mysql_connect($server, $usernamedb, $passworddb);
if ($this->id)
{
$this->selectDB($dbname);
$this->setUtf8();
}
else
{
$this->warning();
}
}
function setUtf8()
{
$this->execute("SET NAMES 'utf8' ");
$this->execute("SET CHARACTER SET 'utf8' ");
}但我在我的php配置default_charset = iso-8859-1中观察到一件事,这是导致问题的原因。
数据库是UTF8格式,所以没有问题。
谢谢。Mona
发布于 2013-11-06 18:25:23
您必须使用mysqli::set_charset将连接字符集设置为utf8。请检查$RSSarticles数组的内容是否真的是utf8的-如果不是,您必须首先自己转换它。
发布于 2013-11-06 18:25:58
编辑以匹配更新:
可以,您应该将配置设置default_charset设置为UTF-8。始终尝试在应用程序的所有部分(网站代码、客户端代码、数据库)中只使用一个字符集
原始答案如下:
在处理UTF-8、PHP和数据库时,您已经发现了一个比较模糊的障碍。
我假设你的输入数据是UTF-8格式的?
即使您的PHP脚本、DB表和字段是UTF-8格式的,您仍然需要告诉DB适配器在传输和连接上使用UTF-8。
实现这一点的最简单方法是在建立数据库连接之后使用SET NAMES语句。
$DB->execute("SET NAMES 'utf8'");应该足够了
发布于 2013-11-06 18:30:39
Kindly set your database default character set to utf8
using following command
ALTER DATABASE db_name
DEFAULT CHARACTER SET utf8
and also set character set in php file using following code in first line of php
<?php
header('Content-Type: text/html; charset=utf-8');
?>https://stackoverflow.com/questions/19809316
复制相似问题