PHP imports CVS into MySQL

Source: Internet
Author: User
  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 the Ampty field at the beginning of these records?
  11. /* This was useful if you had a table with the first field being an auto_increment integer
  12. /* and the CSV file does not having such as empty field before the records.
  13. /* Set 1 for Yes and 0 for No. Attention:don ' t set to 1 if is not sure.
  14. /* This can-dump data in the wrong field if this extra-does not exist in the table
  15. /********************************/
  16. $addauto = 0;
  17. /********************************/
  18. /* would to save the MySQL queries in a file? If Yes set $save to 1.
  19. /* Permission on the file should is 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 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 is 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. $file 2 = fopen ($outputfile, "w");
  71. if (! $file 2) {
  72. echo "Error writing to the output file.\n";
  73. }
  74. else {
  75. Fwrite ($file 2, $queries);
  76. Fclose ($file 2);
  77. }
  78. }
  79. }
  80. echo "Found A total of $lines records in this CSV file.\n";
  81. ?>
Copy Code
Import to, PHP, CVS
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.