2012-12-17 200 views
0

需要添加额外的一行以跳过csv文件中标题的第一行。但我不知道从哪里开始。在导入时跳过第一行csv

<?php 

if(isset($_POST["submit"])) 
{ 
$host="localhost"; // Host name. 
$db_user="root"; //mysql user 
$db_password=""; //mysql pass 
$db='local'; // Database name. 
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error()); 
mysql_select_db($db) or die (mysql_error()); 

echo $filename=$_FILES["file"]["name"]; 
$ext=substr($filename,strrpos($filename,"."),(strlen($filename)-strrpos($filename,"."))); 

    $file = fopen($filename, "r"); 
    $handle = fopen("$filename", "r"); 
    while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) 
    { 
    $import="INSERT into customers(fname,lname,company,address,city,state,country,postal_code,phone,email) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')"; 
    mysql_query($import) or die(mysql_error()); 
    } 
    fclose($handle); 
    print "Import done"; 
    } 
    else 
    { 
    print "<form enctype='multipart/form-data' method='post'>"; 
    print "Type file name to import:"; 
    print "<input type='file' name='file' id='file'>"; 
    print "<input type='submit' name='submit' value='submit'></form>"; 
    } 
?> 

任何帮助是apreciated。

+0

相关:[?如何使标题行使用fgetcsv我while循环跳过](http://stackoverflow.com/questions/4409911/how-使用-full-loop-using-fgetcsv) – hakre

+1

MySQL可以导入带有LOAD DATA INFILE的CSV文件 –

回答

2

fgetcsv($handle, 100000, ",")之前while循环

$file = fopen($filename, "r"); 
    $handle = fopen("$filename", "r"); 

    $headers = fgetcsv($handle, 100000, ","); //gran headers, 

    // you can do additional check to see if headers are grabbed or is the exist or if it is first lien of data (depends if you are 100% sure that headers will exist) 
    //and if they are not, reset the handle 
    // rewind($handle); 


    while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) 
    { 
    $import="INSERT into customers(fname,lname,company,address,city,state,country,postal_code,phone,email) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')"; 
    mysql_query($import) or die(mysql_error()); 
    } 
    fclose($handle); 
    print "Import done"; 
    } 
    else 
    { 
    print "<form enctype='multipart/form-data' method='post'>"; 
    print "Type file name to import:"; 
    print "<input type='file' name='file' id='file'>"; 
    print "<input type='submit' name='submit' value='submit'></form>"; 
    } 
+0

完美,谢谢Elijan – Dar

10

考虑使用SplFileObjectread CSV files,它支持迭代更好,例如与标准SPL LimitIterator结合,这是一块蛋糕:

$file = new SplFileObject($filename); 
$file->setFlags(SplFileObject::READ_CSV); 
$it = new LimitIterator($file, 1); 
foreach($it as $data) { 
    $mask = "INSERT INTO customers (fname, lname, company, address, city, state, country, postal_code, phone, email) values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"; 
    $sql = vsprintf($mask, $data); 
    mysql_query($sql) or die(mysql_error()); 
} 

另外请注意:

Please, don't use mysql_* functions in new code。他们不再维护and are officially deprecated。查看red box?请改为了解prepared statements,并使用PDOMySQLi - this article将帮助您决定哪个。如果您选择PDO,here is a good tutorial

+0

不错,但我该如何整合这对我的脚本?,支撑什么数据需要在里面? – Dar

+0

@Dario:'foreach'循环替换你的代码中的while循环。 – hakre

+0

注意:未定义的偏移量:对于所有行,为什么?,是的,我知道PDO,这是一个测试脚本,然后我将集成在Codeigniter中。 – Dar

0

简单的计数可以正常工作

$count = 0; 
while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) 
{ 
    if($count) 
    { 
     $import="INSERT into customers(fname,lname,company,address,city,state,country,postal_code,phone,email) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]')"; 
     mysql_query($import) or die(mysql_error()); 
    } 
    $count++; 
}