php执行sql文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?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 ){ echo iconv('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 ){ echo iconv('UTF-8', 'UTF-8' ,$sql); echo "<br>"; mysql_query($sql); } } ?> |