青野 发表于 2017-3-26 10:23:54

php执行sql文件

<?php
//http://blog.163.com/tonylee@126/blog/static/130335554201172412923695/先保证表的编码是UTF-8
header('Content-Type: text/html; charset=UTF-8');
$hostname = 'localhost';
$dbname = 'aaa';
$username = 'aaa';
$pw = 'aaa';
$conn = mysql_connect ( $hostname, $username, $pw ) or die ( "无法连接数据库" );
mysql_select_db ( $dbname, $conn ) or die ( "无法连接到数据库" );
mysql_query('set names utf8' );
$arr = array("testdata");//删除数据
foreach($arr as $sqlfile){
$sqlfile=$sqlfile.".sql";
echo $sqlfile."<br>";
$sql = file_get_contents ( $sqlfile );
$ar = split ( ";", join ( "", file ( $sqlfile ) ) );
foreach ( $ar as $sql ){
echoiconv('UTF-8', 'UTF-8' ,$sql);
echo "<br>";
mysql_query($sql);
}
}
$arr = array("system_site","system_nvg","system_art_type","system_art","system_pic",
"system_message","system_auth","system_user","system_group","system_user_group",
"system_i18n","system_access","system_friend");//每个表的insert sql
foreach($arr as $sqlfile){
$sqlfile=$sqlfile.".sql";
echo $sqlfile."<br>";
$sql = file_get_contents ( $sqlfile );
$ar = split ( ";", join ( "", file ( $sqlfile ) ) );
foreach ( $ar as $sql ){
echoiconv('UTF-8', 'UTF-8' ,$sql);
echo "<br>";
mysql_query($sql);
}
}

?>
页: [1]
查看完整版本: php执行sql文件