Load Data into MySQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Load Data into MySQL

Post by AliasBDI »

I am trying to load data from a comma delimited TXT file into a MySQL database (version MySQL 4.0.17-nt). Here is my code:

Code: Select all

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

## Select the database to insert to ##
mysql_select_db('econtrol_canaanloanscom') 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 var_zipcodes 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);
?>
The page returns this:
Error loading data file.
The used command is not allowed with this MySQL version
Any ideas on how to resolve this?


feyd | stripped user/pass
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

Get your poassword and username out of your code AliasBDI..

Ps : I think you should use only one slash

Code: Select all

TERMINATED BY "\\r\\n"
should be

Code: Select all

TERMINATED BY "\r\n"
(it's a wild guess i m not sure)
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Same result. Thanks though.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

As an altenative to 'Load Data .." you could always read the file contents into an array. Then loop thru the array and "explode" each line. At which point you could insert the 'fields'/data into the table.

Lite...
User avatar
sulen
Forum Commoner
Posts: 79
Joined: Wed Jul 09, 2003 4:55 pm
Location: los angeles
Contact:

Post by sulen »

Why dont you use LOAD DATA INFILE instead ........... you must have already tried it but maybe that could work............. :!:
Post Reply