简体   繁体   中英

Load Data Local Infile not adding data to db

For some reason this function is not loading data into my DB. I'm running these test through MAMP.

This is the processing page, which is supposed to to take file input and process into DB.

// Connect to database
$connect = mysql_connect('localhost','nopassword','nopassword');
mysql_select_db('gwt_extract', $connect);

if(!($connect))
{
    die ("Unable to connect to database. Program aborted.");
}

$company_id = $_POST['select_company'];

// Upload files
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Allow certain file formats
if($imageFileType != "csv") {
    echo "Sorry, only CSV files are allowed.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
}
else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

$insert = "LOAD DATA LOCAL INFILE 'uploads/filename.csv'
INTO TABLE raw_data
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS 
(id,date,query,impression,impression_change,clicks,clicks_change,ctr,ctr_change,avg_position,avg_position_change,@company_id)
SET company_id = 1";

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

And my table structure is as follows:

id  int(11)
date    date
query   varchar(100)
impression  int(6)
impression_change   varchar(5)
clicks  int(6)
clicks_change   varchar(5)
ctr varchar(4)
ctr_change  varchar(5)
avg_position    varchar(5)
avg_position_change varchar(5)
company_id int(11)

Looks like in this scenario, looks like my problem was in the LINES TERMINATED line. I changed to this and the issue was resolved:

LINES TERMINATED BY '\r'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM