0

以下のコードは機能しますが、csv ファイルの 2 行目からデータベースに値を挿入したいと考えています。どんなポインタでも素晴らしいでしょう。

$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 に答える