Copying from CSV to MySQL

Codes here !

Moderators: egami, macek, gesf

Post Reply
montyonthebonty
New php-forum User
New php-forum User
Posts: 2
Joined: Thu May 03, 2018 11:05 am

Thu Jul 05, 2018 1:44 pm

HI

I'm trying to write a PHP Script to copy from a CSV file uploaded to the server into a MySQL Database. BUT
  • The CSV file is produced by a third-party program and (for some bizarre reason) and there are no headings. Instead the format is "heading 1, heading 2, data 1, data 2 etc. There are about a dozen headings so effectively I need to disregard the first dozen columns
  • I would like to filter out some of the rows based on different criteria. There are about ten criteria I want to use
I have managed to cobble together the following from various help sites, but I don't know how to ignore columns and how to make sure the right columns go to the right fields in the database.

Can anybody help?

Thanks
Chris

Code: Select all

  // Connect to database
  $conn = new mysqli($servername, $username, $password, $dbname);

  // Check connection
  if ($conn->connect_error) {
    die ("Connection failed: " . $conn->connect_error);
  }

  $sql = "LOAD DATA INFILE 'uploads/digest raw data.csv'
          INTO TABLE tblUncovered
          FIELDS TERMINATED BY ','
          OPTIONALLY ENCLOSED BY '"' 
          LINES TERMINATED BY ',,,\r\n'
          (date, name, type, number, duration, addr, pin, city, state, country, lat, log)";
?>

Post Reply