PHP中的MySQL数据库查询编码问题

时间:2011-04-28 18:15:40

标签: php mysql character-encoding

您好我有这个问题:我的MySQL数据库有整理 latin1_bin 。在数据库中,我有一个排序 utf8_slovak_ci 的表。所有列都是文本。在行中,我有ťíľš(utf8)字符的值,但PHP代码返回值,其中utf8字符替换为 ,?和其他不可读的字符。在html头我已将字符集设置为utf-8 。请帮我。我有这段代码:

//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>");
}

2 个答案:

答案 0 :(得分:3)

运行查询

mysql_query( "set names utf8" );

在运行任何其他查询之前。这将设置连接字符集

http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

答案 1 :(得分:2)

在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;