MySQL数据库查询在PHP
问题描述:
编码问题您好,我有这样的问题:我有一个整理latin1_bin MySQL数据库。在数据库中,我有整理utf8_slovak_ci表。所有collumns是文本。在我行有T IžŁšC(UTF8)字符值,但取代PHP代码返回用UTF-8字符值?和其他不可读的字符。在HTML头我已经设置字符集为UTF-8。请帮帮我。我有这样的代码:MySQL数据库查询在PHP
//From config file
define ("DATABASE_SERVER", "localhost");
define ("DATABASE_LOGIN", "root");
define ("DATABASE_PASSWORD", "root");
define ("DATABASE_DATABASE", "novymost");
mysql_connect(DATABASE_SERVER, DATABASE_LOGIN, DATABASE_PASSWORD)or die(mysql_error());
mysql_select_db("novymost")or die(mysql_error());
$results=mysql_query("SELECT * FROM Downloads") or die(mysql_error());
$row = mysql_fetch_array($result);
// Print out the contents of the entry
while($rows = mysql_fetch_array($results))
{
echo("<li>");
echo "<a href=\"".$rows["URL"];
echo("\">");
echo "".$rows["TITLE"];
echo(" - ".$rows["SIZE"]);
echo("</a>");
echo("<br/></li>");
}
答
运行查询
mysql_query("set names utf8");
runnign任何其他查询之前。这将设置连接字符集
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
+0
工作。谢谢! – 2011-04-28 18:31:01
答
在PHP中:
header("Content-Type: text/html; charset=UTF-8");
在HTML:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
在数据库查询之前:
mysql_query("set names utf8");
或者你也可以拉丁字母表转换你到一个UTF8表:
INSERT INTO utf8table (utf8column)
SELECT CONVERT(latin1field USING utf8)
FROM latin1table;
什么是您的DOCTYPE是什么样子? – fredley 2011-04-28 18:17:41
'' – 2011-04-28 18:18:49