作者eric97979 (WAWAWA忘れ物♪)
看板PHP
標題[請益] PHP讀取&存入資料庫的問題..
時間Fri Nov 21 14:22:16 2008
昨天嘗試用PHP讀取一個txt檔,然後將讀到的字串存入變數
最後連結資料庫 並將變數值放進資料表。
可是不管怎樣存,資料表內的tuple永遠顯示空的 囧"
大略的程式碼如下:
<?
$txtFile = fopen("C:/temp.txt","r");
if ($txtFile)
{
while (!feof($txtFile)) {
//$tem = fgets($txtFile);
$time = fgets($txtFile);
$title = fgets($txtFile);
$Link = fgets($txtFile);
$uTime = fgets($txtFile);
$cut = fgets($txtFile);
echo $time;
echo $title;
echo $Link;
echo $uTime;
echo "<br />";
}
fclose($txtFile);
if(!($database = mysql_connect("localhost","eric97979","123")))
{
die("Can not connect database! <br />");
}
//連接到資料庫
if(!mysql_select_db("webproject",$database))
{
die("Can not open course database! <br />");
}
$insert = "INSERT INTO news VALUES('$time','$title','$Link','$uTime')";
$result = mysql_query($insert,$database);
}
else
{
echo "error";
}
?>
其中"temp.txt"是存在本機內的文字檔,內容存有中文字。
$time、$Link等那些變數echo出來以後 確定裡面的值都OK
但如果網頁編碼用utf-8,txt檔讀出來的中文會變成亂碼
改成BIG5也有問題,因為MySQL是設定utf-8..。
所以主要有2個問題:
第一個是編碼 不知道如何解決orz
第二個問題是存取資料庫的問題(找不到哪邊錯誤)
煩請板上高手能幫忙解惑 感恩QQ
--
◤ ◥ 唧唧唧唧唧唧......
噓▊ ◥◥● ▏
だ ◣ ◣ ◢
!! ◢
◣◢◣▁▁▁
◢▆▆◣▃▃▃ << ひぐらしの
なく頃に >>
▄▄ ,◥_▁▁_______
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.100.248
1F:推 appleboy46 :display_errors = On 先打開 11/21 14:34
2F:→ eric97979 :請問要在哪邊開呢@@? 11/21 14:37
3F:推 hied :如果你的txt是big5 那用iconv或mb_convert_encoding 11/21 14:37
4F:→ hied :把中文字轉成utf8 11/21 14:37
5F:→ eric97979 :OK 先嘗試看看 感謝~ 11/21 14:52