In php, it is relatively simple to import the SQL file generated by phpmyadmin.

Source: Internet
Author: User

We know that many forums or personal blog programs downloaded from the internet have installed pages. To create such installation programs, you need to use SQL files to create databases.

Only valid for SQL files exported by phpmyadmin
Copy codeThe Code is as follows:
$ Dbfile = "test. SQL ";
$ Content = iconv ("UTF-8", "GB2312", file_get_contents ($ dbfile ));
// Obtain the created data
// Remove comments
$ Content = preg_replace ("/--. * \ n/iU", "", $ content );
// Replace the prefix
$ Content = str_replace ("ct _", TABLE_PRE, $ content );

$ Carr = array ();
$ Iarr = array ();
// Extract create
Preg_match_all ("/Create table. * \ (. * \). * \;/iUs", $ content, $ carr );
$ Carr = $ carr [0];
Foreach ($ carr as $ c)
{
@ Mysql_query ($ c, $ link );
}

// Extract insert
Preg_match_all ("/insert into. * \ (. * \) \;/iUs", $ content, $ iarr );
$ Iarr = $ iarr [0];
// Insert data
Foreach ($ iarr as $ c)
{
@ Mysql_query ($ c, $ link );
}

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.