php read sql file into the database (support phpmyadmin export)

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array backup database backup export file function import information

php tutorial to import sql file into the database tutorial (support phpmyadmin export)
Like this php read sql file into the database, the most used is the database backup and restore to the principle is very simple, according to the specified format into a .sql file or use phpmyadmin export can be used to achieve the import process Oh.
* /

function into_sql ($ file)
{
global $ mysql Tutorial _host, $ mysql_user, $ mysql_password, $ mysql_db, $ mysql_table_prefix, $ dbcharset; / / Get the database configuration information
mysql_connect ($ mysql_host, $ mysql_user, $ mysql_password);
mysql_select_db ($ mysql_db);

if (mysql_get_server_info () <'4.1')
// Returns the server version used by link_identifier. If you omit link_identifier, use the last open connection.
{
$ dbcharset = ''; / / set character set, if the mysql version is lower than 4.1, do not set the character set information
}
if (empty ($ dbcharset))
{
$ dbcharset = 'gbk';
}
$ dbcharset && mysql_query ("set names '$ dbcharset'"); // Set 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 database table prefix with the user-defined prefix

$ file2 = explode ("n", $ file2); // read the contents of the file by line into the array
$ c1 = count ($ file2);
for ($ j = 0; $ j <$ c1; $ j ++)
{
$ ck = substr ($ file2 [$ j], 0,4); // Take the first 4 characters of each line
if (ereg ("#", $ ck) || ereg ("-", $ ck)) // remove the comment
{
continue
}
$ arr [] = $ file2 [$ j]; / / remove the contents of the comment file by line into the array $ arr, the array of each element corresponds to a line
}
$ read = implode ("n", $ arr); / / reorganize the contents of the file to a string, (according to the original good line by line)
$ sql = str_replace ("r", '', $ read); / / Remove the "r (carriage return)"
$ detail = explode ("; n", $ sql);
// Will be the contents of the file after the above re-press a complete sql statement (separated by; and n) into the array $ detail,
// 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
$ sql = str_replace ("n", '', $ sql); / / remove the newline
$ sql = trim ($ sql); / / Remove the spaces before and after
// Now $ sql
if ($ sql)
{
if (eregi ("create table", $ sql)) / / If the current sql statement is to create a new table, consider version compatibility, and reset 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')
{
$ sql = preg_replace ("/ engine = myisam / is", "type = myisam", $ sql); //
}
}
mysql_query ($ sql);
}
}
}
?>

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.