stemp Posted July 31, 2007 Share Posted July 31, 2007 I need to build an application in which a user uploads a CSV file through a web form and the application dumps the data it into a mySQL DB. The mySQL DB will be set up the same as the CSV file as far as number of fields and data types. I guess I would just need to have the application format the CSV into the format that mySQL reads right? Any suggestions would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/62688-coding-suggestions/ Share on other sites More sharing options...
deadimp Posted July 31, 2007 Share Posted July 31, 2007 It's not so much that you reformat it to MySQL data format manually, but that you parse the raw data and send it off to MySQL using the API. It wouldn't be that hard to write yourself a parser for CSV, just use something like explode() in the PHP Function library. Quote Link to comment https://forums.phpfreaks.com/topic/62688-coding-suggestions/#findComment-312051 Share on other sites More sharing options...
Psycho Posted July 31, 2007 Share Posted July 31, 2007 I'd suggest using fgetcsv() to read the file into an array - each line is an element. Then separate the fields into elements using explode. Then after validating the information for each field, run a query to insert the data. <?php $handle = fopen("test.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $name = $data[0]; $address = $data[1]; $phone = $data[2]; //Validate the date //Run query to insert record } close($handle); ?> Also, depending how many records there will be you could increase the efficiencly by using the loop to create a single insert query instead of running multiple queries. Quote Link to comment https://forums.phpfreaks.com/topic/62688-coding-suggestions/#findComment-312070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.