PHP将cvs导入到MySql

WBOY
Libérer: 2016-07-25 08:46:13
original
992 Les gens l'ont consulté
  1. $databasehost = "localhost";
  2. $databasename = "test";
  3. $databasetable = "sample";
  4. $databaseusername ="test";
  5. $databasepassword = "";
  6. $fieldseparator = ",";
  7. $lineseparator = "\n";
  8. $csvfile = "filename.csv";
  9. /********************************/
  10. /* Would you like to add an ampty field at the beginning of these records?
  11. /* This is useful if you have a table with the first field being an auto_increment integer
  12. /* and the csv file does not have such as empty field before the records.
  13. /* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
  14. /* This can dump data in the wrong fields if this extra field does not exist in the table
  15. /********************************/
  16. $addauto = 0;
  17. /********************************/
  18. /* Would you like to save the mysql queries in a file? If yes set $save to 1.
  19. /* Permission on the file should be set to 777. Either upload a sample file through ftp and
  20. /* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
  21. /********************************/
  22. $save = 1;
  23. $outputfile = "output.sql";
  24. /********************************/
  25. if (!file_exists($csvfile)) {
  26. echo "File not found. Make sure you specified the correct path.\n";
  27. exit;
  28. }
  29. $file = fopen($csvfile,"r");
  30. if (!$file) {
  31. echo "Error opening data file.\n";
  32. exit;
  33. }
  34. $size = filesize($csvfile);
  35. if (!$size) {
  36. echo "File is empty.\n";
  37. exit;
  38. }
  39. $csvcontent = fread($file,$size);
  40. fclose($file);
  41. $con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
  42. @mysql_select_db($databasename) or die(mysql_error());
  43. $lines = 0;
  44. $queries = "";
  45. $linearray = array();
  46. foreach(split($lineseparator,$csvcontent) as $line) {
  47. $lines++;
  48. $line = trim($line," \t");
  49. $line = str_replace("\r","",$line);
  50. /************************************
  51. This line escapes the special character. remove it if entries are already escaped in the csv file
  52. ************************************/
  53. $line = str_replace("'","\'",$line);
  54. /*************************************/
  55. $linearray = explode($fieldseparator,$line);
  56. $linemysql = implode("','",$linearray);
  57. if($addauto)
  58. $query = "insert into $databasetable values('','$linemysql');";
  59. else
  60. $query = "insert into $databasetable values('$linemysql');";
  61. $queries .= $query . "\n";
  62. @mysql_query($query);
  63. }
  64. @mysql_close($con);
  65. if ($save) {
  66. if (!is_writable($outputfile)) {
  67. echo "File is not writable, check permissions.\n";
  68. }
  69. else {
  70. $file2 = fopen($outputfile,"w");
  71. if(!$file2) {
  72. echo "Error writing to the output file.\n";
  73. }
  74. else {
  75. fwrite($file2,$queries);
  76. fclose($file2);
  77. }
  78. }
  79. }
  80. echo "Found a total of $lines records in this csv file.\n";
  81. ?>
复制代码

导入到, PHP, cvs


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!