MySQL import program

Source: Internet
Author: User
Tags mysql import

<?
// Used for fast MySQL Big Data Backup
// Modify the SQL file name, Database Host Name, database user name, password, and database name to be imported according to the code comment before use.
// Simultaneously export the database file and the text file together with FTP to the website directory, and then access the file in Web mode.
// If the website owner is out of date (www.im286.com), the website owner is copyrighted and can be used at will, but the copyright information is retained.

$ File_name = "SQL. SQL"; // name of the SQL file to be imported
$ Dbhost = "localhost"; // Database Host Name
$ Dbuser = "user"; // database username
$ Dbpass = "pass"; // Database Password
$ Dbname = "dbname"; // Database Name

Set_time_limit (0); // set the timeout value to 0, indicating that the operation is always executed. If PHP is invalid in safe mode, the import may time out. In this case, you need to import data in multiple parts.
$ Fp = @ fopen ($ file_name, "R") or die ("SQL File $ file_name" cannot be opened); // open the file
Mysql_connect ($ dbhost, $ dbuser, $ dbpass) or die ("cannot connect to database $ dbhost"); // connect to database
Mysql_select_db ($ dbname) or die ("You cannot open the database $ dbname"); // open the database
Mysql_query ('set names utf8 ');
Echo "importing operation in progress ";
While ($ SQL = getnextsql ()){
If (! Mysql_query ($ SQL )){
Echo "execution error:". mysql_error ()."
";
Echo "SQL statement:
". $ SQL ."
";
};
}
Echo "Import completed ";

Fclose ($ FP) or die ("can't close file $ file_name"); // close the file
Mysql_close ();

// Obtain the SQL statement from the file one by one
Function getnextsql (){
Global $ FP;
$ SQL = "";
While ($ line = @ fgets ($ FP, 40960 )){
$ Line = trim ($ line );
// The following three statements are not required in the PHP version. You may need to modify them in some earlier versions.
$ Line = str_replace ("//", "//", $ line );
$ Line = str_replace ("/'", "'", $ line );
$ Line = str_replace ("// R // n", CHR (13). CHR (10), $ line );
// $ Line = stripcslashes ($ line );
If (strlen ($ line)> 1 ){
If ($ line [0] = "-" & $ line [1] = "-"){
Continue;
}
}
$ SQL. = $ line. CHR (13). CHR (10 );
If (strlen ($ line)> 0 ){
If ($ line [strlen ($ line)-1] = ";"){
Break;
}
}
}
Return $ SQL;
}
?>

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lee3f/archive/2008/01/10/2034121.aspx

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.