CSV数据库输入为每列导致一行

时间:2014-07-24 17:08:48

标签: php mysql csv

我已成功将数据从csv传入数据库但由于某种原因,查询产生的结果与csv中相同数量的列相同7倍。

<?php

$host="localhost";
$user="general";
$pass="gretna22607";
$db="general";
$con=mysqli_connect($host, $user, $pass) or die ('Error connecting to mysql' . mysqli__error());
mysqli_select_db($con,$db) or die ('Error connecting to database' . mysqli__error());
$empty="TRUNCATE csvimport";
mysqli_query($con,$empty);

$csvdata = file_get_contents('http://www.overclockers.co.uk/productfeed/b2b_cs312.csv');

$csv_file="ocuk.csv";

if (($getfile = fopen($csv_file, "r")) !== FALSE)
    {
        while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE)
            {
                $num = count($data);
                for ($a=0; $a < $num; $a++)
                    {
                        $results=$data;
                        $str = implode(",", $results);
                        $slice = explode(",", $str);
                        $col1 = $slice[0];
                        $col2 = $slice[1];
                        $col3 = $slice[2];
                                            $col4 = $slice[3];
                                            $col5 = $slice[4];
                                            $col6 = $slice[5];
                                            $col7 = $slice[6];

                        // SQL Query to insert data into DataBase

                        //$query = "INSERT INTO csvimport (tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7) VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')".";";
                        //$query = "INSERT INTO csvimport VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."','','','')".";";
                        //mysqli_query($con,$query);
                    }
                $query = "INSERT INTO csvimport (tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7) VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."')".";";
                                 mysqli_query($con,$query);
            }
    }
fclose($csv_file);
mysqli_close();

?>

例如

如果我回显查询

第一个就像这样

insert into csvimport ('test','test','test','','','','');
insert into csvimport ('test2','test2','test2','','','','');
insert into csvimport ('test3','test3','test3','','','','');
insert into csvimport ('test4','test4','test4','','','','');
insert into csvimport ('test','test','test','','','','');
insert into csvimport ('test','test','test','','','','');
insert into csvimport ('test2','test2','test2','','','','');
insert into csvimport ('test2','test2','test2','','','','');

我希望它只需将csv的每一行输入一次并将其输入到包含所有7列的数据库中

1 个答案:

答案 0 :(得分:1)

删除此行:

$data = fgetcsv($getfile, 1000, ",");

然后移动这些行:

$query = "INSERT INTO csvimport (tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7) VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$co$
//$query = "INSERT INTO csvimport VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."','','','')".";";
mysqli_query($con,$query);

for loop

之外