Forum Moderators: coopster

Message Too Old, No Replies

uploading info from a text file into a MySQL database

a short php script to do this?

         

mylungsarempty

6:11 pm on Jan 14, 2004 (gmt 0)

10+ Year Member



what would the PHP and SQL code be to say basically: make a new row and put this in the field, what comes after each comma goes in a new field, and each new line should be a new row.

I have a huge text file that i don't have a way to upload directly into a MySQL database because my webhost isn't allowing it for some reason.

coopster

6:34 pm on Jan 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Typically I FTP the .txt file to the server then load the data Using mysql in Batch Mode [mysql.com].

mysql -p database < textfile.txt

Another option is Loading Data into a Table [mysql.com] using the LOAD DATA statement from the command line interface after starting mysql:

mysql> USE database;
mysql> LOAD DATA LOCAL INFILE "textfile.txt" INTO TABLE table;

If neither of these options is available, you're right, you may have to roll your own "import data" script.

ergophobe

9:34 pm on Jan 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



most hosts that don't give shell access usually allow you to use phpMyAdmin. Doesn't that have a file upload feature?

If you have a test server, you can put the data into it, get it the way you want, then use mysql dump to get it out. Then upload the dump file.

As I write that, though, it starts to sound like more trouble than writing a script to import directly, depending on your situation.

Tom

jatar_k

9:55 pm on Jan 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I have had a couple of situations where the dump file was just way too big. It crashed phpmyadmin as well as causing mysql to bomb part way through the file.

What I ended up doing was uploading via ftp the dumpfile.sql and then logging into the shell and doing this

mysql -u username -p < /path/to/dumpfile.sql

and that worked flawlessly. The only other option I had would be to chop it up into manageable pieces and I didn't feel like cutting up 2,000,000 rows manually.

mylungsarempty

5:25 pm on Jan 15, 2004 (gmt 0)

10+ Year Member



Well, my particular host (i've spoken with tech support) has disabled the feature for some reason? They are really giving me a hard time with this, and i'm ready to switch hosting companies. A friend is currently writing the script for me that will insert the data from the .txt file into a database for me.