0

我有下面的代码可以工作,但我想从我的 csv 文件的第二行将值插入到我的数据库中。请任何指针都会很棒。

$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());

$row = 1; 
if (($handle = fopen($csvfile, "r")) !== FALSE) { 
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
$num = count($data); 
echo "<p> $num fields in line $row: <br /></p>\n"; 
$row++; 


for ($c=0; $c < $num; $c++) { 
echo $data[$c] . "<br />\n"; 
           } 

$query = "INSERT into $databasetable(email,emailSource,espBanner,firstName,lastName,Address,City,
State,zip,home,mobile,card,ethnicity,language,gender,filename,is_uploaded)
                values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]'
,'$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]',
'$data[14]','$data[15]','$data[16]')";

 mysql_query($query) or die(mysql_error());

     } 
 fclose($handle); 


 }

结果将插入 csv 的每一行。

4

1 回答 1

0
$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());

$start_from_row = 2;
$row = 0;
if (($handle = fopen($csvfile, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br></p>\n";
        $row++;
        if($row < $start_from_row)
            continue;

        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br>\n";
        }

        $query = "INSERT into $databasetable(email,emailSource,espBanner,firstName,lastName,Address,City,
        State,zip,home,mobile,card,ethnicity,language,gender,filename,is_uploaded)
        values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]'
        ,'$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]',
        '$data[14]','$data[15]','$data[16]')";

        mysql_query($query) or die(mysql_error());

    }
    fclose($handle);


}
于 2012-07-01T01:41:29.260 回答