加载数据Infile - 从PHP脚本不起作用

时间:2016-11-03 16:00:03

标签: php mysql sql

我编写了一个脚本来将CSV文件导入mySQL数据库。但是,当我尝试从PHP脚本运行SQL时,它不起作用。当我将SQL查询放入phpMyAdmin时,它确实有效。

$server = 'localhost';
$username = 'root';
$password = 'password123';
$database = 'product';

$connect = new mysqli($server, $username, $password, $database);

if($connect->connect_error) {
    die('Connection Failed' . $connection->connect_error);
}

$files = scandir('imports/');

foreach($files as $file) {
    if ($file != '.' && $file != '..') {
        $import =
            "LOAD DATA INFILE 'imports/$file'
            IGNORE INTO TABLE temp_import
            FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
            LINES TERMINATED BY '\n'
            IGNORE 1 LINES
            (@deal_id, @redeemed_at, @wowcher_code, @deal_title, @customer_name, @house_name_number, @address_line_1, @address_line_2,
        @city, @county, @postcode, @email, @phone, @date_of_birth, @custom_field, @marketing_permission, @product_name, @product_options)
        set deal_id=@deal_id, redeemed_at=@redeemed_at, wowcher_code=@wowcher_code, deal_title=@deal_title, customer_name=@customer_name,
        house_name_number=@house_name_number, address_line_1=@address_line_1, address_line_2=@address_line_2, city=@city, county=@county,
        postcode=@postcode, email=@email, phone=@phone, date_of_birth=@date_of_birth, custom_field=@custom_field,
        marketing_permission=@marketing_permission, product_name=@product_name, product_options=@product_options";

        if(! $connect->query($import)) {
            echo 'Failed: ' . $file . PHP_EOL;
        }

        else {
            $connect->query($import);

            echo mysqli_affected_rows($connect) . PHP_EOL;
        }
    }
}

任何人都可以看到为什么这可能无法正常工作

0 个答案:

没有答案