PHP read SQL file Import Database (support phpMyAdmin export) _php tutorial

Source: Internet
Author: User
Tags ereg import database
PHP read SQL file Import Database (support phpMyAdmin export) like this PHP read SQL file Import database situation, the most used is the database backup and restore, The principle is simple to import into a. sql file in the specified format or use the phpMyAdmin export can be implemented using this program to implement the import Oh.

PHP Tutorial Read SQL file Import Database Tutorial (support phpMyAdmin Export)
Like this PHP read SQL file Import database, the most used is the database backup and restore, the principle is simple to import into. sql file in the specified format or use phpMyAdmin export can use this program to implement the import Oh.
*/

function Into_sql ($file)
{
Global $mysql Tutorials _host, $mysql _user, $mysql _password, $mysql _db, $mysql _table_prefix, $dbcharset;//Get 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 Link_identifier is omitted, the previous open connection is used.
{
$dbcharset = ';//sets the character set, if the MySQL version is less than 4.1, does not set the character set information
}
if (empty ($dbcharset))
{
$dbcharset = ' GBK ';
}
$dbcharset && mysql_query ("Set names ' $dbcharset '");//Set character sets
if (Mysql_get_server_info () > ' 5.0 ')
{
mysql_query ("Set sql_mode=");
}
$file 2=file_get_contents ($file);
$file 2=iconv ("Utf-8", "GBK", $file 2);
$file 2=str_replace ("seo Tutorial _", $mysql _table_prefix, $file 2);//replace the database table prefix in the file with a user-defined prefix

$file 2=explode ("n", $file 2);//reads the contents of the file into the array by line
$c 1=count ($file 2);
for ($j =0; $j < $c 1; $j + +)
{
$ck =substr ($file 2[$j],0,4);//Take the first 4 characters of each line
if (Ereg ("#", $ck) | | Ereg ("--", $ck))//Remove annotations
{
Continue
}
$arr []= $file 2[$j];//reads the file contents of the comment into the array $arr, each element of the array corresponds to a row
}
$read =implode ("n", $arr); Reorganize the contents of a file into a string (in line with the original line)
$sql =str_replace ("R", "', $read);//Remove" R (Carriage return) "
$detail =explode ("; n", $sql);
Import the file contents of the above collation into the array $detail again by a complete SQL statement (; and n Delimited).
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 carriage return from each line of SQL
$sql =str_replace ("n", "', $sql);//Remove line break
$sql =trim ($sql);//Remove space before and after
Now the $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);
}
}
}

?>

http://www.bkjia.com/PHPjc/630792.html www.bkjia.com true http://www.bkjia.com/PHPjc/630792.html techarticle PHP Read SQL file Import Database (support phpMyAdmin export) like this PHP read SQL file Import database situation, the most used is the database backup and restore, the principle is very simple to press ...

  • 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.