The simpler method in php is to import the sql file generated by phpmyadmin

  • 2020-05-09 18:13:53
  • OfStack

We know that many forums or personal blog applications that are downloaded from the Internet have installed pages. To create such an installation, you need to use the sql file to create a database.

Only valid for sql files exported by phpmyadmin
 
$dbfile="test.sql"; 
$content=iconv("UTF-8","GB2312",file_get_contents($dbfile)); 
// Gets the data created  
// Remove the comment  
$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); 
} 

Related articles: