Php reads SQL files and imports them to the database

Source: Internet
Author: User
For example, when php reads SQL files and imports them to a database, the most commonly used data is database backup and restoration. The principle is very simple. you can use this program to import SQL files or export data using phpmyadmin. the code is as follows :? P... for example, when php reads SQL files and imports them to a database, the most commonly used data is database backup and restoration. The principle is very simple. you can use this program to import SQL files or export data using phpmyadmin. the code is as follows:

 '5. 0 ') {mysql_query ("set SQL _mode ='' ") ;}$ file2 = file_get_contents ($ file); $ file2 = iconv (" UTF-8 "," gbk ", $ file2); $ file2 = str_replace ("seo tutorial _", $ mysql_table_prefix, $ file2 ); // replace the database table prefix in the file with the user-defined prefix $ file2 = explode ("n", $ file2 ); // read the file content into the array by row $ c1 = count ($ file2); for ($ j = 0; $ j <$ c1; $ j ++) {$ ck = substr ($ file2 [$ j], 0, 4); // obtain the first 4 characters of each line if (ereg ("#", $ ck) | ereg ("--", $ ck) // remove the annotation {continue;} $ arr [] = $ file2 [$ j]; // read the uncommented file content into the array $ arr by row. each element in the array corresponds to a row} $ read = implode ("n", $ arr ); // re-organize the file content to a string (based on the original row) $ SQL = str_replace ("r", '', $ read ); // remove "r (carriage return)" $ detail = explode ("; n", $ SQL ); // import the content of the file that has been sorted out to the array $ detail by a complete SQL statement (separated by; and n, // at this time, each element of the array detail corresponds to a complete SQL statement $ count = count ($ detail); for ($ I = 0; $ I <$ count; $ I ++) {$ SQL = str_replace ("r", '', $ detail [$ I]); // remove the carriage return in each SQL line $ SQL = str_replace ("n", '', $ SQL); // remove the line break $ SQL = trim ($ SQL ); // Remove leading and trailing spaces // the current $ SQL if ($ SQL) {if (eregi ("create table", $ SQL )) // if the current SQL statement is to create a new table, consider version compatibility and resetting the character set {// $ mysqlv = mysql_get_server_info (); $ SQL = preg_replace ("/default charset = ([a-z0-9] +)/is", "", $ SQL ); // remove the original character set setting information $ SQL = preg_replace ("/type = myisam/is", "engine = myisam", $ SQL); if ($ dbcharset) {$ SQL = str_replace ("engine = myisam", "engine = myisam default charset = $ dbcharset", $ SQL);} if (mysql_get_server_info () <'4. 1 ') {// open source code phprm.com $ SQL = preg_replace ("/engine = myisam/is", "type = myisam", $ SQL ); //} mysql_query ($ SQL );}}}


Permanent address:

Reprint at will ~ Please bring the tutorial URL ^

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.