PHP import CSV to MySQL database example

Source: Internet
Author: User
  1. /**

  2. * Import CSV file into MySQL database
  3. * edit:bbs.it-home.org
  4. */
  5. $databasehost = "localhost";
  6. $databasename = "Test";
  7. $databasetable = "Sample";
  8. $databaseusername = "Test";
  9. $databasepassword = "";
  10. $fieldseparator = ",";
  11. $lineseparator = "\ n";
  12. $csvfile = "Filename.csv";

  13. /********************************/

  14. /* Would the Ampty field at the beginning of these records?
  15. /* This was useful if you had a table with the first field being an auto_increment integer
  16. /* and the CSV file does not having such as empty field before the records.
  17. /* Set 1 for Yes and 0 for No. Attention:don ' t set to 1 if is not sure.
  18. /* This can-dump data in the wrong field if this extra-does not exist in the table
  19. /********************************/
  20. $addauto = 0;
  21. /********************************/

  22. /* would to save the MySQL queries in a file? If Yes set $save to 1.

  23. /* Permission on the file should is set to 777. Either upload a sample file through FTP and
  24. /* Change the permissions, or execute at the Prompt:touch output.sql && chmod 777 Output.sql
  25. /********************************/
  26. $save = 1;
  27. $outputfile = "Output.sql";
  28. /********************************/

  29. if (!file_exists ($csvfile)) {

  30. echo "File not found. Make sure specified the correct path.\n ";
  31. Exit
  32. }

  33. $file = fopen ($csvfile, "R");

  34. if (! $file) {

  35. echo "Error opening data file.\n";
  36. Exit
  37. }

  38. $size = FileSize ($csvfile);

  39. if (! $size) {

  40. echo "File is empty.\n";
  41. Exit
  42. }

  43. $csvcontent = Fread ($file, $size);

  44. Fclose ($file);

  45. $con = @mysql_connect ($databasehost, $databaseusername, $databasepassword) or Die (Mysql_error ());

  46. @mysql_select_db ($databasename) or Die (Mysql_error ());

  47. $lines = 0;

  48. $queries = "";
  49. $linearray = Array ();

  50. foreach (Split ($lineseparator, $csvcontent) as $line) {

  51. $lines + +;

  52. $line = Trim ($line, "\ t");

  53. $line = Str_replace ("\ R", "", $line);

  54. /************************************

  55. This line escapes the special character. Remove it if entries is already escaped in the CSV file
  56. ************************************/
  57. $line = Str_replace ("'", "\ '", $line);
  58. /*************************************/

  59. $linearray = Explode ($fieldseparator, $line);

  60. $linemysql = Implode ("', '", $linearray);

  61. if ($addauto)

  62. $query = "INSERT into $databasetable values (', ' $linemysql ');";
  63. Else
  64. $query = "INSERT into $databasetable values (' $linemysql ');";

  65. $queries. = $query. "\ n";

  66. @mysql_query ($query);

  67. }

  68. @mysql_close ($con);

  69. if ($save) {

  70. if (!is_writable ($outputfile)) {

  71. echo "File is not writable, check permissions.\n";
  72. }

  73. else {

  74. $file 2 = fopen ($outputfile, "w");

  75. if (! $file 2) {

  76. echo "Error writing to the output file.\n";
  77. }
  78. else {
  79. Fwrite ($file 2, $queries);
  80. Fclose ($file 2);
  81. }
  82. }

  83. }

  84. echo "How many $lines records are found in the CSV file. \ n";
  85. ?>

Copy Code
  • 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.