importing excel to mysql db


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $file = fopen("myfile.csv", "r");
  2. $sql = "INSERT INTO table(field1, field2, field3...";
  3. while(!feof($file))
  4. {
  5. $fields = explode(',', fgets($file));
  6. $sql .= "VALUES("
  7. foreach($fields as $field)
  8. {
  9. $sql .= "$field, ";
  10. }
  11. $sql = substr($sql, 0, -2);
  12. $sql .= "), ";
  13. }
  14. $sql = substr($sql, 0, -2);
  15. fclose($file);
  16. mysql_query($sql);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.