首頁 > 後端開發 > php教程 > PHP將cvs導入到MySql

PHP將cvs導入到MySql

WBOY
發布: 2016-07-25 08:46:13
原創
1016 人瀏覽過
  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


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板