我創建了以下腳本來查詢表並返回前30個結果。查詢返回30個結果,但它們沒有任何文本或信息。爲什麼會這樣?爲什麼我在使用php查詢mysql時返回空記錄?
該表存儲越南文字符。數據庫是mysql4。
這裏的頁面:http://saomaidanang.com/recentposts.php
下面的代碼:
<?php
header('Content-Type: text/html; charset=utf-8');
//CONNECTION INFO
$dbms = 'mysql';
$dbhost = 'xxxxx';
$dbname = 'xxxxxxx';
$dbuser = 'xxxxxxx';
$dbpasswd = 'xxxxxxxxxxxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpasswd) or die('Error connecting to mysql');
mysql_select_db($dbname , $conn);
//QUERY
$result = mysql_query("SET NAMES utf8");
$cmd = 'SELECT * FROM `phpbb_posts_text` ORDER BY `phpbb_posts_text`.`post_subject` DESC LIMIT 0, 30 ';
$result = mysql_query($cmd);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr">
<head>
<title>recent posts</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p>
<?php
//DISPLAY
while ($myrow = mysql_fetch_row($result))
{
echo 'post subject:';
echo(utf8_encode($myrow ['post_subject']));
echo 'post text:';
echo(utf8_encode($myrow ['post_text']));
}
?>
</p>
</body>
爲什麼要使用函數utf8_encode?這似乎很有必要 – 2010-03-09 06:46:39
@Col。 Shrapnel:因爲我不知道我在做什麼? 說真的,我使用它,因爲這個stackoverflow問題。 http://stackoverflow.com/questions/1198701他有一個類似的問題,utf8_encode解決了它。 – 2010-03-09 13:43:48
正如我所見,他說SET NAMES utf8做了詭計。 嘗試你的代碼沒有utf8_encode,它應該工作正常。 你已經設置好了一切,不需要額外的重新編碼。 – 2010-03-09 14:09:00