簡單的 PHP 將sql文件導入數據庫程序

<?php



header("content-Type: text/html; charset=UTF-8");



$conn=mysql_connect('localhost','root','123');//指定数据库连接参数

if (!$conn){die('Could not connect: ' . mysql_error());}

mysql_select_db('test',$conn);

mysql_query('SET NAMES "UTF8"');



$sql_file="test1.sql";

$content=file_get_contents($sql_file);//获取创建的数据

$content=preg_replace("/--.*n/iu","",$content);//去掉注释



$carr = $iarr = array();

//提取create

preg_match_all("/create table[^;]+?;/i",$content,$carr);

//創建數據表

foreach($carr[0] as $c){

    @mysql_query($c,$conn);

}



//提取insert

preg_match_all("/insert into[^;]+?;/i",$content,$iarr);

//插入数据

foreach($iarr[0] as $c){

    @mysql_query($c,$conn);

}



?>

 

你可能感兴趣的:(sql)