[請益] PHP讀取&存入資料庫的問題..

看板PHP作者 (WAWAWA忘れ物♪)時間17年前 (2008/11/21 14:22), 編輯推噓2(203)
留言5則, 3人參與, 最新討論串1/1
昨天嘗試用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

11/21 14:34, , 1F
display_errors = On 先打開
11/21 14:34, 1F

11/21 14:37, , 2F
請問要在哪邊開呢@@?
11/21 14:37, 2F

11/21 14:37, , 3F
如果你的txt是big5 那用iconv或mb_convert_encoding
11/21 14:37, 3F

11/21 14:37, , 4F
把中文字轉成utf8
11/21 14:37, 4F

11/21 14:52, , 5F
OK 先嘗試看看 感謝~
11/21 14:52, 5F
文章代碼(AID): #199bET3x (PHP)
文章代碼(AID): #199bET3x (PHP)