Jump to content

fgetcsv writing empty records


affordit

Recommended Posts

I have ascript that loops through a directory of zip files, unzips to new directory and deletes the zip file and is supposed to import to mysql database. Everything works BUT writes empty records.

Here is my code if anyone can help would be great

<?php
include("sharons_dbinfo.inc.php");
$files = glob('gz/*.gz');
$texts = glob('*.txt');

foreach ($files as $in_file) {

$file=str_replace(".gz","",$in_file);
$file=str_replace("gz/","",$file);
$file2=fopen($file,"w");


$gz = gzopen($in_file, 'r');
while (!gzeof($gz)) {
$contents = gzread($gz, 10000);
fwrite($file2, $contents);

}
gzclose($gz);
unlink ($in_file);

foreach ($texts as $text) {
$handle = fopen("$text", "r");

     while (($data = fgetcsv($handle, 10000, ",")) !== FALSE)

     {
$data = str_replace("'", "", $data);
    
mysql_connect(localhost,$user,$password);
mysql_select_db($database) or die( "Unable to select database");

       $import="INSERT into new (`ID`,`PROGRAMNAME`,`PROGRAMURL`,`CATALOGNAME`,`LASTUPDATED`,`NAME`,`KEYWORDS`,`DESCRIPTION`,`SKU`,`MANUFACTURER`,`MANUFACTURERID`,`UPC`,`ISBN`,`CURRENCY`,`SALEPRICE`,`PRICE`,`RETAILPRICE`,`FROMPRICE`,`BUYURL`,`IMPRESSIONURL`,`IMAGEURL`,`ADVERTISERCATEGORY`,`THIRDPARTYID`,`THIRDPARTYCATEGORY`,`AUTHOR`,`ARTIST`,`TITLE`,`PUBLISHER`,`LABEL`,`FORMAT`,`SPECIAL`,`GIFT`,`PROMOTIONALTEXT`,`STARTDATE`,`ENDDATE`,`OFFLINE`,`ONLINE`,`INSTOCK`,`CONDITION`,`WARRANTY`,`STANDARDSHIPPINGCOST`) values('','$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]','$data[12]','$data[13]','$data[14]','$data[15]','$data[16]','$data[17]','$data[18]','$data[19]','$data[20]','$data[21]','$data[22]','$data[23]','$data[24]','$data[25]','$data[26]','$data[27]','$data[28]','$data[29]','$data[30]','$data[31]','$data[32]','$data[33]','$data[34]','$data[35]','$data[36]','$data[37]','$data[38]','$data[39]')";

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


     }

fclose($handle);


}
}
?>

Thanks everyone

Link to comment
Share on other sites

Should have been

$handle = fopen($text, "r");

Now works great. I have to update over 2.000.000 products per week from zipped csv files and this will unzip and load all the records to the database in less than 5 minutes. It has been taking me about 8-9 hours to do this.

Love php

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.