proba1 Posted May 22, 2015 Share Posted May 22, 2015 Hello guys i am new here. I have troubles with inserting data from csv file. I want to insert from csv file data but distribution of columns is not in the same order like in database. For example : table: contact columns: contact_first,contact_last, contact_email ,but in csv file order is in the opposite order : contact_email,contact_last,contact_first or doesn't matter can be in random order. Thanks in advance, Maja Quote Link to comment https://forums.phpfreaks.com/topic/296446-importing-data-with-csv-files/ Share on other sites More sharing options...
Barand Posted May 22, 2015 Share Posted May 22, 2015 It would only matter if you are using LOAD DATA INFILE. If you are you need to specify the columns at the end of the statement Quote Link to comment https://forums.phpfreaks.com/topic/296446-importing-data-with-csv-files/#findComment-1512430 Share on other sites More sharing options...
proba1 Posted May 22, 2015 Author Share Posted May 22, 2015 No i'm not using LOAD DATA INFILE , I'm using fgetcsv() function for inserting. Quote Link to comment https://forums.phpfreaks.com/topic/296446-importing-data-with-csv-files/#findComment-1512433 Share on other sites More sharing options...
ginerjm Posted May 22, 2015 Share Posted May 22, 2015 Actually you are using fgetcsv to READ the incoming data. As I said in my other response to you, the incoming data must be in a recognizable order or you can not do this. Whether the data has identifiers within it or there is a row of headers at the start, either way. But to be completely random is not possible. I have to wonder how this data was prepared for you with no thought given as to how it would be used. Quote Link to comment https://forums.phpfreaks.com/topic/296446-importing-data-with-csv-files/#findComment-1512434 Share on other sites More sharing options...
proba1 Posted May 22, 2015 Author Share Posted May 22, 2015 Thank you ginerjm, somehow we solve the problem. Yeah you are right about the incoming data that should be in recognizable order, cause otherwise it will through an exception report . Quote Link to comment https://forums.phpfreaks.com/topic/296446-importing-data-with-csv-files/#findComment-1512448 Share on other sites More sharing options...
Barand Posted May 22, 2015 Share Posted May 22, 2015 $csv = fopen('my.csv', 'r'); $data = array(); while (list($contact_email, $contact_last, $contact_first) = fgetcsv($csv, 1024)) { $data[] = "('$contact_first', '$contact_last', '$contact_email')"; // order matches SQL INSERT order } fclose($csv); // use multiple insert $sql = "INSERT INTO contact (contact_first,contact_last, contact_email) VALUES "; $sql .= join(',', $data); $mysqli->query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/296446-importing-data-with-csv-files/#findComment-1512476 Share on other sites More sharing options...
ginerjm Posted May 22, 2015 Share Posted May 22, 2015 Barand has provided some great code that normally would be perfect for you Proba. But as you said originally the input can be in 'random order' so this code will not work. Are you sure the input is in "random order"? Seems strange to have a process that produces random input like you describe. Quote Link to comment https://forums.phpfreaks.com/topic/296446-importing-data-with-csv-files/#findComment-1512483 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.