PHP import cvs to MySql

WBOY
Release: 2016-07-25 08:46:13
Original
953 people have browsed it
  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. This line escapes the special character. remove it if entries are already escaped in the csv file
  54. *****************************************/
  55. $line = str_replace("'","'",$line);
  56. /&#&********/
  57. $linearray = explode($fieldseparator,$line);
  58. $linemysql = implode("','",$linearray);
  59. if($addauto)
  60. $query = "insert into $databasetable values('','$linemysql');";
  61. else
  62. $query = "insert into $databasetable values('$linemysql');";
  63. $queries .= $query . "n";
  64. @mysql_query($query);
  65. }
  66. @mysql_close($con);
  67. if ($save) {
  68. if (!is_writable($outputfile)) {
  69. echo "File is not writable, check permissions.n";
  70. }
  71. else {
  72. $file2 = fopen($outputfile,"w");
  73. if(!$file2) {
  74. echo "Error writing to the output file.n";
  75. }
  76. else {
  77. fwrite($file2,$queries);
  78. fclose($file2);
  79. }
  80. }
  81. }
  82. echo "Found a total of $lines records in this csv file.n";
?>
复制代码
导入到, PHP, cvs


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!