我尝试用PHP从.txt文件加载数据,但每次都失败。我尝试将文本文件转换为CSV,但即使这样也失败了。我的文本文件包含如下内容:
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
我在分隔符方面有问题。
发布于 2011-08-10 20:39:55
假设您的输入文件如下所示,下面的代码将会工作。这就是:
发布于 2011-08-10 18:52:46
您正在尝试使用|字符explode
每一行,但我在文本中没有看到任何|!PHP代码看起来没问题(未经测试)。需要处理的是文本输入。
https://stackoverflow.com/questions/7009390
复制相似问题