Jump to content

Help parsing csv file


mbk

Recommended Posts

Hi,

 

I am trying to parse a csv file and insert the contents into a mysql table.  I could use the insert local file option, but I dont want all the columns importing.  I have the following code, but this doesnt seem to work.

 

<?php

$table = 'suppliers';

$username = "user";
$pass = 'pass';
$db = mysql_connect("localhost", $username, $pass);
mysql_select_db("testdb",$db); 

$empty_table = "TRUNCATE TABLE $table";
mysql_query($empty_table) or die (mysql_error());

$file_handle = fopen("file.csv", "r");

while (!feof($file_handle) ) {

$tmp = fgetcsv($file_handle, 8192);

$category = ($tmp[0]);
$supp_pn = ($tmp[1]);
$desc = ($tmp[3]);
$help = ($tmp[5]);

$sql = "INSERT INTO $table SET category='$category', supp_pn='$supp_pn', desc='$desc', $help='help'";
mysql_query($sql)or die (mysql_error());

}

fclose($file_handle);

?>

 

An example of the data in the csv is below -

"","FUNDHD      ","","FUNDHD      ","",0.00,0.00,0

"[Pending Assignment]","ATS0057     ","                                        ","AT-FS705LE-30       ","Hama UK Ltd                   ",17.31,29.80,0

"[Pending Assignment]","MARKETING   ","                                        ","MARKETING   ","Hama UK Ltd                   ",20000.00,30000.00,0

 

Can someone help pls?

Link to comment
Share on other sites

Thanks for the quick reply - I made the amend and now get the following error..

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc)VALUES('', 'FUNDHD      ', 'FUNDHD      ')' at line 1

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.