Php reads SQL files and imports them to the database (supports phpmyadmin export) _ PHP Tutorial

Source: Internet
Author: User
Tags ereg mysql tutorial import database
Php reads SQL files and imports them to the database (phpmyadmin export is supported ). Php reads SQL files and imports them to the database (phpmyadmin can be exported). For example, if php reads SQL files and imports them to the database, database backup and restoration are the most commonly used, the principle is very simple. read SQL files by php and import them to the database (phpmyadmin is supported). For example, if php reads SQL files and imports them to the database, database backup and restoration are the most commonly used, the principle is very simple. you can use this program to import SQL files or export them using phpmyadmin.

Php Tutorial reading SQL file import database tutorial (phpmyadmin export supported)
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 them using phpmyadmin.
*/

Function pai_ SQL ($ file)
{
Global $ mysql tutorial _ host, $ mysql_user, $ mysql_password, $ mysql_db, $ mysql_table_prefix, $ dbcharset; // Obtain database configuration information
Mysql_connect ($ mysql_host, $ mysql_user, $ mysql_password );
Mysql_select_db ($ mysql_db );

If (mysql_get_server_info () <'4. 1 ')
// Return the server version used by link_identifier. If link_identifier is omitted, the last opened connection is used.
{
$ Dbcharset = ''; // set the character set. if the mysql version is earlier than 4.1, the character set information is not set.
}
If (empty ($ dbcharset ))
{
$ Dbcharset = 'gbk ';
}
$ Dbcharset & mysql_query ("set names '$ dbcharset'"); // sets the character set
If (mysql_get_server_info ()> '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 prefix of the database table in the file with the prefix set by the user.

$ 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],); // obtain the first 4 characters of each line
If (ereg ("#", $ ck) | ereg ("--", $ ck) // remove the comment
{
Continue;
}
$ Arr [] = $ file2 [$ j]; // read the uncommented file content into the array by row $ arr. 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 line and line)
$ 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 characters in each SQL line.
$ SQL = str_replace ("n", '', $ SQL); // remove the line break
$ SQL = trim ($ SQL); // remove spaces
// Current $ SQL
If ($ SQL)
{
If (eregi ("create table", $ SQL) // if the current SQL statement is to create a new table, version compatibility and character set resetting are considered.
{
// $ Mysqlv = mysql_get_server_info ();
$ SQL = preg_replace ("/default charset = ([a-z0-9] +)/is", "", $ SQL); // Remove original character set settings
$ 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 ')
{
$ SQL = preg_replace ("/engine = myisam/is", "type = myisam", $ SQL );//
}
}
Mysql_query ($ SQL );
}
}
}

?>

Export (supports phpmyadmin export). For example, when using php to read SQL files and import them to the database, database backup and restoration are the most commonly used. The principle is very simple...

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.