php reads sql files and imports them into the database (supports phpmyadmin export) In situations like this where PHP reads SQL files and imports them into the database, the most commonly used method is database backup and restoration. The principle is very simple and can be imported into a .sql file according to the specified format or exported using phpmyadmin. You can use this program to achieve the import.
php tutorial to read sql file and import database tutorial (supports phpmyadmin export)
In situations like this where PHP reads SQL files and imports them into the database, the most commonly used method is database backup and restoration. The principle is very simple and can be imported into a .sql file according to the specified format or exported using phpmyadmin. You can use this program to achieve the import.
*/
function into_sql($file)
{
global $mysql tutorial_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' )
//Return the server version used by link_identifier. If link_identifier is omitted, the last open connection is used.
{
$dbcharset='';//Set the character set. If the mysql version is lower than 4.1, the character set information will not be set
}
if(empty($dbcharset))
{
$dbcharset='gbk';
}
$dbcharset && mysql_query("set names '$dbcharset'");//Set 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 in the file with the prefix set by the user
$file2=explode("n",$file2);//Read the file content into the array line by line
$c1=count($file2);
for($j=0;$j<$c1;$j++)
{
$ck=substr($file2[$j],0,4);//Get the first 4 characters of each line
If( ereg("#",$ck)||ereg("--",$ck) )//Remove comments
{
continue;
}
$arr[]=$file2[$j];//Read the uncommented file contents into the array $arr line by line, each element of the array corresponds to one line
}
$read=implode("n",$arr); //Reorganize the file content into a string (line by line according to the original division)
$sql=str_replace("r",'',$read);//Remove "r (carriage return character)"
$detail=explode(";n",$sql);
//Import the above-organized file contents into the array $detail,
again as 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
in each line of sql $sql=str_replace("n",'',$sql);//Remove the newline character
$sql=trim($sql);//Remove leading and trailing spaces
//Current $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);
}
}
}
?>