Jump to content

[SOLVED] CSV to MySQL Script - PHP


iceblox

Recommended Posts

Hi guys,

 

Im having trouble using this csv to mysql script everytime i excute the file on my server it says the follwoing 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 'table (Date, Price) VALUES ('11-May,89.99 ', '')' at line 1

 

This is the code..

 

 

<?php

$connection = mysql_connect('localhost','username','pass');

mysql_select_db('mobile');

$file = 'DatePrice.csv'; //file name here

//we read the CSV file here
$lines = file($file);

//now we take each line and explode it then insert to DB
foreach ($lines as $line) {
$cols = explode(';', $line);
mysql_query("INSERT INTO table (Date, Price) VALUES ('$cols[0]', '$cols[1]')")or 
die(mysql_error());
}

echo mysql_affected_rows();

?>

 

Not to sure what is causing the error. Any one got any ideas?

Link to comment
https://forums.phpfreaks.com/topic/60737-solved-csv-to-mysql-script-php/
Share on other sites

Hmm

 

A few suggestions:

 

1. Is your table name really "table"??? Off the top of my head i thought that was a reserved word?? try "mytable" or something.

 

2. you should always trim values when reading:

$cols = explode(';', trim($line));

 

3. What are your column definitions, do you specify NOT NULL for price?

 

Hope something there helps you...

 

cheers,

tdw

Archived

This topic is now archived and is closed to further replies.

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