PHP imports large amounts of data to MySQL (sample)

Source: Internet
Author: User
  1. Fast MySQL Big Data backup

  2. Before using, first modify the SQL file name, database hostname, database user name, password, database name to import by following the code comments.
  3. Both the database file and the text are FTP-guided through the Web site directory, and then Web Access to the file is
  4. edit:bbs.it-home.org
  5. $file _name= "Bn_site.sql";//SQL file name to import
  6. $dbhost = "localhost";//Database host name
  7. $dbuser = "root";//Database user name
  8. $dbpass = "";//Database Password
  9. $dbname = "Bn_site"; Database name
  10. Set_time_limit (0);//Set the timeout to 0, indicating that it has been executed. When PHP is invalid in safe mode, a time-out is imported and a multipart import is required
  11. $fp = @fopen ($file _name, "R") or Die ("Cannot open SQL file");//Open File
  12. Mysql_connect ($dbhost, $dbuser, $dbpass) or Die ("Cannot connect to database"); Connecting to a database
  13. mysql_select_db ($dbname) or Die ("Cannot open database");//Open database
  14. echo "Performing the import operation";
  15. while ($SQL =getnextsql ()) {
  16. if (!mysql_query ($sql)) {
  17. echo "Execution error:". Mysql_error (). "
    ";
  18. The echo "SQL statement is:
    ". $SQL."
    ";
  19. };

  20. }

  21. echo "Import complete";

  22. Fclose ($fp) or Die ("can" T close file $file "); Close File

  23. Mysql_close ();
  24. Fetch SQL from File
  25. function Getnetsql () {
  26. Global $fp;
  27. $sql = "";
  28. while ($line = @fgets ($fp, 40960)) {
  29. $line =trim ($line);
  30. The third sentence is not required in the high version of PHP and may need to be modified in some lower versions
  31. $line = Str_replace ("\\\\", "\ \", $line);
  32. $line = Str_replace ("\ '", "'", $line);
  33. $line = Str_replace ("\\r\\n", Chr. chr), $line);
  34. if (strlen ($line) >1) {
  35. if ($line [0]== "-" && $line [1]== "-") {
  36. Continue
  37. }
  38. }

  39. $sql. = $line. chr (10);

  40. if (strlen ($line) >0) {
  41. if ($line [strlen ($line) -1]== ";") {
  42. Break
  43. }
  44. }
  45. }
  46. return $sql;
  47. }
  48. ?>

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.