When creating a website, we will create an installation file and use the SQL file to create a database. Share my methods.
When creating a website, we will create an installation file and use the SQL file to create a database. Share my methods.
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
The 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 );
}