Jump to content

eugenevz

New Members
  • Posts

    1
  • Joined

  • Last visited

eugenevz's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Hi I have a script which picks up CSV files and inserts into a table (mysql). I am a beginner and finding it difficult to get the script to add an additional column to existing CSV, and add value to column, for each row... Here is the script.... How can I manipulate $csv and add a column (including separator) and value ($filedate) for each row? Help much appreciated. for example: CSV contains: Number, Name, LastName 1, John, Thomas 2, Jeffrey, James I need to add: Number, Name, LastName, Date 1, John, Thomas, 2014-10-31 2, Jeffrey, James, 2014-10-31 <?php #! /usr/bin/env php $databasehost = "localhost"; $databasename = "****"; $databasetable = "****"; $databaseusername="****"; $databasepassword = "****"; $fieldseparator = ","; $lineseparator = "\n"; $files = glob("*ETF_*.csv"); $filedate = "2014-10-31"; if (empty($files)) { die("No records were loaded."); } else foreach($files as $csv) $etf_array = array( PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); { try { $pdo = new PDO("mysql:host=$databasehost;dbname=$databasename", $databaseusername, $databasepassword, $etf_array ); } catch (PDOException $e) { die("database connection failed: ".$e->getMessage()); } $affectedRows = $pdo->exec(" LOAD DATA LOCAL INFILE ".$pdo->quote($csv)." INTO TABLE `$databasetable` FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)." LINES TERMINATED BY ".$pdo->quote($lineseparator)." IGNORE 1 LINES"); echo "Daily - Loaded a total of $affectedRows records from $csv.\n"; } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.