使用PHP将文本文件插入MySQL
我试图用PHP从.txt文件加载数据,但每次都失败了。 我试图将文本文件转换为CSV,但即使在那里我也失败了。 我的文本文件包含如下内容:使用PHP将文本文件插入MySQL,php,mysql,text-files,delimiter,Php,Mysql,Text Files,Delimiter,我试图用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
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());
}
?>
我在使用分隔符时遇到问题。您试图用|字符分解每行,但我在文本中没有看到任何|!PHP代码看起来正常(未经测试)。需要处理的是文本输入。假设您的输入文件如上所示,下面的代码可以工作。即:
- 每个记录包含3个字段
- 文件中用空行分隔的字段
- 文件中可被3整除的字段数
<?php /* For simplicity in this example, I will assume the db table * conforms to the following schema: * * CREATE TABLE products( * id VARCHAR(5), * item VARCHAR(100), * item_details TEXT * ); */ /* Make the db connection */ $link = mysql_connect('localhost', 'mysql_user', 'user_pwd'); if (!$link) { die('Could not connect to db.'); } // set the db. $db_selected = mysql_select_db('test', $link); if (!$db_selected) { die ("Can't access the db"); } /* Read entire file into string. */ $fileContents = file_get_contents('f1.txt'); /* Set line ending character based on particular OS. * Unix = \n * Macintosh = \r * MS Windows = \r\n */ $lec = "\r\n"; // My system is MS Windows. /*Fields are separated by a blank line so the delimiter between * fields is double line ending character ($lec). */ $delimiter = "$lec$lec"; /* Build array of field data for all records. */ $contentArray = explode($delimiter, $fileContents); /* Get the size of the array. */ $numElements = count($contentArray); /* Because we have 3 fields per record, $numElements should be * evenly divisible by 3. */ $numElelemtsPerRec = 3; if($numElements % $numElelemtsPerRec > 0) exit("File parse error. Contents of file does not fill records."); /* Now build SQL input records. * One record contails 3 ($numElelemtsPerRec) array elements. */ for ($i = 0; $i <= $numElements - 1; $i = $i + $numElelemtsPerRec) { $id = mysql_real_escape_string($contentArray[$i]); $item = mysql_real_escape_string($contentArray[$i+1]); $item_details = mysql_real_escape_string($contentArray[$i+2]); $records .= "('$id', '$item', '$item_details'), "; echo("id = $id<br> item = $item<br> item_details = $item_details<br> <hr>"); } /* Now trim the ending comma and space from string of records. */ $records = substr($records, 0, -2); //exit($records); /* Build and execute the SQL INSERT query. */ $query = "INSERT INTO products (id, item, item_details) VALUES $records"; $result = mysql_query($query); /* Check for db error. */ if(!$result) echo("Query Error! Process aborted."); ?>
在实际插入数据库之前,您需要首先使文本文件中的数据结构更好。例如,首先将文本文件处理为数组,然后处理数组的数据以将其插入数据库。这将允许您在将输入数据插入数据库之前验证输入数据。txt文件本身包含逗号是否有问题?(…270度,速度可调…)whitehat,如果您能够准确地发布从给定样本插入数据库的内容。如果您完整地写下包含数据库布局的输出表,这一点就很清楚了。@cem请查看底部的值a、b、c,然后查看文本。应该不会那么难理解。好的,从解决方案中找到答案。所以c将包含所有剩余的数据。是的,它是文本,我以前尝试过不要混淆任何人。谢谢你,一个快乐的极客,一个我认为有点复杂的人,但嘿,它起作用了。
<?php /* For simplicity in this example, I will assume the db table * conforms to the following schema: * * CREATE TABLE products( * id VARCHAR(5), * item VARCHAR(100), * item_details TEXT * ); */ /* Make the db connection */ $link = mysql_connect('localhost', 'mysql_user', 'user_pwd'); if (!$link) { die('Could not connect to db.'); } // set the db. $db_selected = mysql_select_db('test', $link); if (!$db_selected) { die ("Can't access the db"); } /* Read entire file into string. */ $fileContents = file_get_contents('f1.txt'); /* Set line ending character based on particular OS. * Unix = \n * Macintosh = \r * MS Windows = \r\n */ $lec = "\r\n"; // My system is MS Windows. /*Fields are separated by a blank line so the delimiter between * fields is double line ending character ($lec). */ $delimiter = "$lec$lec"; /* Build array of field data for all records. */ $contentArray = explode($delimiter, $fileContents); /* Get the size of the array. */ $numElements = count($contentArray); /* Because we have 3 fields per record, $numElements should be * evenly divisible by 3. */ $numElelemtsPerRec = 3; if($numElements % $numElelemtsPerRec > 0) exit("File parse error. Contents of file does not fill records."); /* Now build SQL input records. * One record contails 3 ($numElelemtsPerRec) array elements. */ for ($i = 0; $i <= $numElements - 1; $i = $i + $numElelemtsPerRec) { $id = mysql_real_escape_string($contentArray[$i]); $item = mysql_real_escape_string($contentArray[$i+1]); $item_details = mysql_real_escape_string($contentArray[$i+2]); $records .= "('$id', '$item', '$item_details'), "; echo("id = $id<br> item = $item<br> item_details = $item_details<br> <hr>"); } /* Now trim the ending comma and space from string of records. */ $records = substr($records, 0, -2); //exit($records); /* Build and execute the SQL INSERT query. */ $query = "INSERT INTO products (id, item, item_details) VALUES $records"; $result = mysql_query($query); /* Check for db error. */ if(!$result) echo("Query Error! Process aborted."); ?>