Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Uploading a CSV file to mysql database from a webform
Old 10-15-2010, 01:53 PM Uploading a CSV file to mysql database from a webform
sepple's Avatar
Super Talker

Posts: 147
Trades: 0
Hi Everyone,

I am going crazy trying to search for solutions to this problem so I thought I'd ask here for help!

I am trying to upload a csv file through a web form and enter that info into a database. Seems like it should be pretty simple and it should be available out there but I've been losing my mind searching for this.

Please let me know if you have any help or if you can point me in the right direction!

Thanks,
Stephen
__________________
Thanks
Stephen
sepple is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-15-2010, 02:15 PM Re: Uploading a CSV file to mysql database from a webform
kids's Avatar
Ultra Talker

Posts: 301
Trades: 0
Try with file process function to read line by line and insert to your database with SQL "INSERT"

That's the way you can do, it's so easy!
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
kids is offline
Reply With Quote
View Public Profile Visit kids's homepage!
 
Old 10-15-2010, 02:38 PM Re: Uploading a CSV file to mysql database from a webform
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
Here's a nifty way to iterate a very large CSV file and import into your DB without loading the whole thing into memory:
PHP Code:
<?php

$len 
4096;
$delimiter ",";
$enclosure "\"";

$conn mysql_connect(/* connection info */);
mysql_query("BEGIN");
try {
    
$lines = new SplFileObject("/home/my/file/path.csv");
    foreach (
$lines as $line) {
        
$fh fopen('php://memory''rw');
        
fwrite($fh$line);
        
rewind($fh);
        
$result fgetcsv$fh$len$delimiter$enclosure );
        
fclose($fh);
        foreach (
$result as &$value) {
            
$value "'".mysql_real_escape_string($value)."'";
        }
        
$values implode(", "$result);
        
mysql_query("INSERT INTO my_table(col1, col2, col3, ...) VALUES ({$values})");
    }
    
mysql_query("COMMIT");
} catch (
Exception $e) {
    
mysql_query("ROLLBACK");
    print(
$e->getMessage());
}
?>
This way, you get each line one at a time as an array, rather than one huge array with the CSV data.

It allows you to parse and handle very large CSV files without running out of memory - I've imported 1.6 GB CSVs (with millions of records) this way!!!

Also has good error handling to either succeed or fail the entire transaction... so you won't get duplicate records if it fails and you have to restart.
Good luck!
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE

Last edited by smoseley; 10-15-2010 at 02:49 PM..
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 10-15-2010, 04:46 PM Re: Uploading a CSV file to mysql database from a webform
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
another possibility, once the file is copied to the server, is to use the
LOAD DATA INFILE command:
http://dev.mysql.com/doc/refman/5.5/en/load-data.html
Quote:
LOAD DATA INFILE can be used to read files obtained from external sources. For example, many programs can export data in comma-separated values (CSV) format, such that lines have fields separated by commas and enclosed within double quotation marks, with an initial line of column names. If the lines in such a file are terminated by carriage return/newline pairs, the statement shown here illustrates the field- and line-handling options you would use to load the file:
Code:
LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name   
FIELDS TERMINATED BY ',' ENCLOSED BY '"'   
LINES TERMINATED BY '\r\n'   IGNORE 1 LINES;
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 12-24-2010, 07:29 AM Re: Uploading a CSV file to mysql database from a webform
Junior Talker

Posts: 1
Name: Musa
Trades: 0
Hi Smoseley,
I've did exactly what you said here, but it still memory limit exceeded error. Here is my code -
PHP Code:
$this->query("BEGIN");
try{
    
    
$lines = new SplFileObject($file_path);
    foreach(
$lines as $key=>$line){
        
        if(
$key==0)
            continue;
        
        
$fh fopen("php://memory""rw");
        
fwrite($fh$line);
        
rewind($fh);
        
$row fgetcsv($fh$len$delimiter$enclosure);
        
fclose($fh);
        
        
$row array_combine($fields$data);
        
$sql =     $this->prepare_sql($table$row);
        
$this->query($sql);
    }

    
$this->query("COMMIT");
    
} catch(
Exception $e){
    
$this->query("ROLLBACK");
    

Any idea???
Musa is offline
Reply With Quote
View Public Profile
 
Old 12-28-2010, 04:10 PM Re: Uploading a CSV file to mysql database from a webform
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
You're running out of memory because of the MySQL Transaction.

Get rid of the lines with BEGIN, COMMIT, and ROLLBACK and see how it works for ya.

Cheers
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Reply     « Reply to Uploading a CSV file to mysql database from a webform
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.18886 seconds with 12 queries