Skip to content
Advertisement

Concatenate my php variable to csv data when importing LOAD DATA INFILE

I need to import some csv files to mysql.

I recently found about the LOAD DATA INFILE, I checked the manual, as far as I understand, to imports each line of .csv file to the database.

I was wondering if there’s an option to concatenate an extra value to what the csv file contains.

For example: my table has 3 columns.

MYTABLE(#name,#email,company_adrs);

The csv file has data for “name,email” only, and I have the address in a php variable.

With a normal loop, I would achieve this as follows:

//open file
while (/* not end of file, read eachline*/)
{
  $current_line_of_csv_file; // contains: "John,john@john.com" without the 3rd column
  $myAddrsVar= mysqli_real_escape_string($db, $_POST['adrs']); //"44 company st" 
  $insert = $db->query("INSERT MYTABLE (name,email,company_adrs) VALUES ( $current_line_of_csv_file,'".$myAddrsVar."')");

}

With this method I have to loop through each line the csv and do a query in each iteration.

I was wondering if LOAD DATA INFILE has a method to concatenate my data to the csv file? if so, how can I achieve it?

Thanks in advance

Advertisement

Answer

Given your situation, I would recommend using PHP all-in-all and insert data into MySQL that way.

https://www.php.net/manual/en/function.str-getcsv.php can help you parse CSV into an array. Add to the array the address. Now, you’d have the dataset in an array.

Use prepared statements like shown here https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection and insert data one after the other.

Use transactions so that all data is inserted or no data is insert. See How to start and end transaction in mysqli?.

I’d avoid using LOAD DATA INLINE because the volume of data is not that big. If you really want to use it and add another column, you can use SET statement. There’s an example of that here: Add extra column of data when using LOAD DATA LOCAL INFILE

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement