我想用PHP加載.txt文件中的數據,但每次都失敗。 我試圖將文本文件轉換爲CSV,但即使在那裏我也失敗了。 我的文本文件包含這樣的內容:用PHP插入文本文件到MySQL
0001
120PCS LED MOVING HEAD LIGHT
TECHNICAL PARAMETER
Voltage: AC90V-120V or 200V-240V 50-60HZ
Power consumption:400W
Light source:120PCS 1W or 3W LED
(R:30pcs?G:30pcs,B:30psc,W:30pcs)
Control mode:12HS
Operation mode: master-slave, auto movement,
Sound control: DMX 512
Each led source has an expectancy over 50000 to 100000 hours in theory
Optical len angle:15 degrees
Level scanning:540 degrees Vertical scanning
270 degrees, speed adjustable
Indefinite RGBW color mixing system
LCD display adopted
Product size:512*402*555mm
N.W:19kg G.W:21kg
0002
LED 108PCS MOVING HEAD LIGHT
TECHNICAL PARAMETER
Voltage:AC100V-240V ,50/60HZ
Power consumption:306W
Light source:108pcs of 1/3W LED
Operation mode master-slave, sound control,
auto movement,DMX512
Control channel:11Hs
Level scanning angle:540 degrees
Vertical scanning angle:270degrees
Quick electronic dimmer, strobe from 1 to 20 times/second
Smooth RGB mixing system &
Rainbow effect(can add white)
Beam angle:15 degrees
Package size :420*330*550mm
N.W:10kg G.W:13kg
我試着使用:
<?php
$handle = @fopen("data.txt", "r");
$conn = mysql_connect("localhost","root","");
mysql_select_db("mytext",$conn);
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096);
// Read a line.
list($a,$b,$c)=explode("|",$buffer);
//Separate string by the means of |
echo $a."-".$b."-".$c."<br>";
$sql = "INSERT INTO data_table (id, name, age) VALUES('".$a."','".$b."','".$c."')";
mysql_query($sql,$conn) or die(mysql_error());
}
?>
但我無法弄清楚如何很好地插入。
value a = 0001
value b = 120PCS LED MOVING HEAD LIGHT
value c = Technicalparameter etc etc etc
value a = 0002
value b = LED 108PCS MOVING HEAD LIGHT
value c = Technicalparameter etc etc etc
我對分隔符有問題。
您需要首先在進行實際的數據庫插入之前,從文本文件中更好地構造數據。首先將文本文件處理爲數組,然後處理數組的數據以將其插入到數據庫中。這將允許您在將輸入數據插入數據庫之前驗證輸入數據。 – hakre
難道這是一個問題,你的TXT文件本身包含逗號? (... 270度,速度可調...) – madc
whitehat如果您可以發佈*完全*應該從給定示例中插入數據庫的內容。如果你完全記下包括數據庫佈局在內的輸出表,那就很清楚了。 –