[英]PHP import using txt file
I am using following code for importing data into database using txt file 我正在使用以下代码使用txt文件将数据导入数据库
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096); // Read a line.
$data_array =explode("|",$buffer);//Separate string by the means of |
$escaped_array = array_map('mysql_escape_string',$data_array);
$escaped_array = '("'.implode('","',$escaped_array).'") ';
if($i !=1) {
echo $sql = 'INSERT INTO '.$tablename.' VALUES'.$escaped_array;
mysql_query($sql) ; //or die(mysql_error().$tablename);// use mysql insert query here
//exit;
}
$i++;
} //end while
it inserting values like INSERT INTO PROVIDER_DEMOGRAPHIC VALUES("10000104","Sand","David","C","MD","Family Practice","","N","N","N","P","125\\r\\r\\n") INSERT INTO PROVIDER_DEMOGRAPHIC VALUES("10000105","Stucky","Mitchell","B","MD","Family Practice","","N","N","N","P","101\\r\\r\\n") 它插入诸如INSERT INTO PROVIDER_DEMOGRAPHIC VALUES(“ 10000104”,“ Sand”,“ David”,“ C”,“ MD”,“家庭实践”,“”,“ N”,“ N”,“ N”,“之类的值P“,” 125 \\ r \\ r \\ n“)插入PROVIDER_DEMOGRAPHIC值(” 10000105“,” Stucky“,” Mitchell“,” B“,” MD“,”家庭实践“,”“,” N“, “N”, “N”, “P”, “101 \\ r \\ r \\ n” 个)
I donot want extra character in last fiels like \\r\\n\\r Please tell me how i can trim it. 我不想在\\ r \\ n \\ r等最后的字段中使用其他字符,请告诉我如何修剪它。
You are reading the newline characters from the file, and may need to trim it 您正在从文件中读取换行符,可能需要对其进行修剪
Use: 采用:
$buffer = fgets($handle, 4096); // Read a line.
$buffer = rtrim($buffer);
$escaped_array = array_map('mysql_escape_string',$data_array);
Warning: mysql_
function is deprecated as of PHP 5.5.0, and will be removed in the future. 警告:从PHP 5.5.0开始不推荐使用
mysql_
函数,以后会删除它。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应使用MySQLi或PDO_MySQL扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.