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
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

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.