【问题标题】:PHP import using txt file使用txt文件导入PHP
【发布时间】:2013-11-28 03:55:20
【问题描述】:

我正在使用以下代码使用 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

它插入像这样的值 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","家庭实践","","N","N", "N","P","101\r\r\n")

我不想在最后的字段中添加额外的字符,例如 \r\n\r 请告诉我如何修剪它。

【问题讨论】:

  • 使用fgetcsv并将分隔符设置为|而不是默认的,

标签: php database import


【解决方案1】:

您正在从文件中读取换行符,可能需要修剪它

用途:

$buffer = fgets($handle, 4096); // Read a line.
$buffer = rtrim($buffer);
$escaped_array = array_map('mysql_escape_string',$data_array);

警告: mysql_ 函数自 PHP 5.5.0 起已弃用,将来将被删除。相反,应使用 MySQLiPDO_MySQL 扩展名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 2019-04-30
    • 2011-11-18
    • 2016-11-18
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多