>  기사  >  백엔드 개발  >  PHP는 cvs를 MySql로 가져옵니다.

PHP는 cvs를 MySql로 가져옵니다.

WBOY
WBOY원래의
2016-07-25 08:46:13992검색
  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로 가져오기


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.