PHP将cvs导入到MySql

WBOY
Freigeben: 2016-07-25 08:46:13
Original
958 Leute haben es durchsucht
  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


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!