Hello.

There is such code:


while ( ($data_f = fgetcsv($handle_f, 1000, ";"))!== FALSE) { $insert_q = 'insert into test (val1,val2,val3,val4) values '. '("'.$data_f[0].'","'.$data_f[1].'","'.$data_f[2].'","'.$data_f[3].'")'; @mysql_query($insert_q); 

Is it possible to alter the query in the database in such a way that instead of a single insert be a group one?

  • @Neek05, no need to click "reward" for any reason! It is enough to press "thumbs up" and if the answer completely suits you, then the checkbox in front of him. - Deonis

1 answer 1

 $query = "INSERT INTO `test` (`val1`,`val2`,`val3`,`val4`) VALUES "; $data = array(); while ( ($data_f = fgetcsv($handle_f, 1000, ";"))!== FALSE) { $data[] = "(".$data_f[0].",".$data_f[1].",".$data_f[2].",".$data_f[3].")"; } $query .= implode(',', $data); 

$ query contains the entire query. It remains only to perform. And if you use an outdated extension to work with the " mysql_ *" database, then a severe @eicto will come and put it in a corner. ;) Use MySQLi or PDO.

  • Interested in this question: We have a file with the extension csv (the size can be both large and small). You need to parse it and write to the database. Would it be more correct to record a single select or is your option more rational? - neek
  • @ Neek05, for your purposes. in my opinion, it would be better to use [LOAD DATA INFILE] [1] [1]: dev.mysql.com/doc/refman/5.1/en/load-data.html - Deonis