Processing Excel / CSV into MySQL with PHP question? [csv excel] [excel csv]

Q: On a daily basis my user demand for information from the website and the format the information to intervene in a CSV / Excel file and insert them in the database. What I would like to do is to create a page where they can copy and paste into the form and send it as an insert in the db .

Any ideas? 01/03/1933

Apache, MySQL and PHP 4.3.11 + peer 4.1.11.

SHUX


Re:Posted by Norman Santos on September 14 2004 8:53am [Delete] [Edit]

When uploading a CSV file exported from MS excel use this setup:

'LOAD DATA INFILE "filename.csv" INTO TABLE your_table FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\\r\\n";'

The { OPTIONALLY ENCLOSED BY """" } part will put fields, with comas in the right place in the table, instead of moving to the next field in the set and generally making a mess of things.
http://dev.mysql.com/doc/mysql/en/load-data.html

Guarenteed to work if you Save As .CSV with excel first. Used it many times before.


Re:i would use excel to export as a CSV first. PHP needs the COM class to work with Excel files otherwise.

Re:OK, I can do that… More specifically could I create something like this?

SHUX


Re:Originally posted by: guy
<?php
## Connect to a local database server (or die) ##
$dbH = mysql_connect('localhost', 'user', 'pass') or die('Could not connect to MySQL server.<br>' . mysql_error());

## Select the database to insert to ##
mysql_select_db('test') or die('Could not select database.<br>' . mysql_error();

## CSV file to read in ##
$CSVFile = 'data.txt';

mysql_query('LOAD DATA LOCAL INFILE "data.txt" INTO TABLE News FIELDS TERMINATED BY "," LINES TERMINATED BY "\\r\\n";') or die('Error loading data file.<br>' . mysql_error());

## Close database connection when finished ##
mysql_close($dbH);
?>

:thumbsup:


Re:<?php
## Connect to a local database server (or die) ##
$dbH = mysql_connect('localhost', 'user', 'pass') or die('Could not connect to MySQL server.<br>' . mysql_error());

## Select the database to insert to ##
mysql_select_db('test') or die('Could not select database.<br>' . mysql_error();

## CSV file to read in ##
$CSVFile = 'data.txt';

mysql_query('LOAD DATA LOCAL INFILE "data.txt" INTO TABLE News FIELDS TERMINATED BY "," LINES TERMINATED BY "\\r\\n";') or die('Error loading data file.<br>' . mysql_error());

## Close database connection when finished ##
mysql_close($dbH);
?>


Related posts

Leave a comment

0 Comments.

Leave a Reply


click to changeSecurity Code

[ Ctrl + Enter ]