0

需要添加一个额外的行来跳过 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>";
    }
?>

任何帮助都将不胜感激。

4

3 回答 3

14

考虑使用SplFileObject读取 CSV 文件,它可以更好地支持迭代,例如与标准 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());
}

另外请注意:

请不要mysql_*在新代码中使用函数。它们不再被维护并被正式弃用。看到红框了吗?改为了解准备好的语句,并使用PDOMySQLi -本文将帮助您决定使用哪个。如果您选择 PDO,这里有一个很好的教程

于 2012-12-17T00:17:08.280 回答
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>";
    }
于 2012-12-17T00:15:28.553 回答
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++;
}
于 2012-12-17T00:19:23.290 回答