Jump to content

PHP, CSV and MySQL import problem :/


Nickmadd

Recommended Posts

Hey guys, I'm having a nightmare at the moment.

 

I am trying to import a CSV file into a MySQL table, some of the data is interested properly into the rows however some of the CSV row do not get imported.

 

Here is my PHP script:

<?php

$databasehost = "localhost";
$databasename = "name";
$databasetable = "import";
$databaseusername="username";
$databasepassword = "password";
$fieldseparator = ",";
$lineseparator = "\n";
$enclosedbyquote = '"';
$csvfile = "db-core/feed/csv/test.csv";

if(!file_exists($csvfile)) {
die("File not found. Make sure you specified the correct path.");
}

try {
$pdo = new PDO("mysql:host=$databasehost;dbname=$databasename",
$databaseusername, $databasepassword,
array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
die("database connection failed: ".$e->getMessage());
}

$pdo->exec("TRUNCATE TABLE `$databasetable`");

$affectedRows = $pdo->exec("
LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." REPLACE INTO TABLE `$databasetable`
FIELDS OPTIONALLY ENCLOSED BY ".$pdo->quote($enclosedbyquote)."
TERMINATED BY ".$pdo->quote($fieldseparator)."
LINES TERMINATED BY ".$pdo->quote($lineseparator)."
IGNORE 1 LINES");

echo "Loaded a total of $affectedRows records from this csv file.\n";

?>

As you can see the script is importing the CSV into the table however only 40 of the rows are added to the SQL table when there are actually 80 rows in the CSV. I am also telling the script to IGNORE 1 LINES if that has anything to do with the issue.

 

Here is one of the rows from the CSV file:

"AETA83919","AETV34458584","SD57VUP","BLACK","Diesel","2007","76778","Hatchback","3","VAUXHALL","CORSA","1.3 CDTi [90] SXi 3dr [AC]","1200","3989","","Manual","http://images.autoexposure.co.uk/AETA83919/AETV34458584_1b.jpg","0","3","","0","2007 57 reg VAUXHALL CORSA 1.3 CDTi [90] SXi 3dr [AC], Manual, Hatchback, Diesel, 76,778 miles. BLACK, Comprehensive Warranty Included In Price. HPI Checked! Pre Delivery Health Check!, 5 seats, LOVELY VAUXHALL CORSA SXI 1.3CDTI SXI WITH AIR CONDITIONING!! VERY WELL CARED FOR AND MAINTAINED AND JUST SERVICED BY DRIVEN CAR SALES LTD AT 76K!! GREAT COMBINATION OF PERFORMANCE AND ECONOMY!! VIEWING AND TEST DRIVING IS RECOMMENDED!! EACH CAR COMES WITH A MININIUM 9 MONTHS MOT. FULL MECHANICAL HEALTH CHECK GUARANTEES CAR IS PREPARED TO THE HIGHEST STANDARD. COMPREHENSIVE RAC WARRANTY INCLUDED. DRIVEN CAR SALES WELCOME ANY INSPECTION. BUY WITH CONFIDENCE AND REASSURANCE AS DRIVEN CAR SALES LTD ARE OFFICIAL MEMBERS OF RETAIL MOTOR INDUSTRY FEDERATION. VIEW OUR REVIEWS ON TRUSTPILOT. 3,989","","0","1","","UK","0","","0","0","","","",""

As far as I can see everything looks fine with the row, all of the rows in the CSV end like this.

 

One thing I have noticed is it looks like the rows that are missing are in fact being placed in the last column of every other row here is an example:

 

 

2014-09-07_1526.png

 

 

So as you can see it looks like one row is being inserted into the last column of some rows and I am so confused to why this might be happening?

 

Any ideas?

 

 

 

Link to comment
Share on other sites

this will create an import table and load your csv

$db = new mysqli(HOST,USERNAME,PASSWORD,'test');

$fp = fopen('DrivenCarSales.csv','r');

$k = 0;
$sizes=array_fill(0,35,0);
$heads = fgetcsv($fp,2048);     // get header row

while ($line = fgetcsv($fp,2048)) {
    // get max field lengths
    for ($i=0; $i<35; $i++) {
        $sizes[$i] = max($sizes[$i], strlen($line[$i]));
    }
}
fclose($fp);

$fields = array_combine($heads,$sizes);

    // drop table if already exists
$db->query("DROP TABLE IF EXISTS import");

    // create the import table
$sql = "CREATE TABLE `import` (\n";
foreach ($fields as $fn=>$sz) {
    $sz+=2;
    $sql .= "`$fn` VARCHAR($sz),\n";
}
$sql .= "PRIMARY KEY (Vehicle_ID)\n)";
$db->query($sql);

//
// now load the data
//
$fp = fopen('DrivenCarSales.csv','r');
$heads = fgetcsv($fp,2048);     // get header row

while ($line = fgetcsv($fp,2048)) {
    $line = array_map(array($db,'real_escape_string'), $line);
    $data = join("','", $line);
    $db->query("INSERT IGNORE INTO `import` VALUES ('$data')") or die($db->error);
}
fclose($fp);

edit: PS your pictureRefs and options fields need normalising. Your field name "condition" is a reserved SQL word

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.