正文 554字数 425,337阅读

<?php $file_name = 'd.sql'; //要导入的SQL文件名 $dbhost = 'localhost'; //数据库主机名 $dbuser = 'root'; //数据库用户名 $dbpass = 'root'; //数据库密码 $dbname = 'test'; //数据库名 $con = @mysql_connect($dbhost,$dbuser,$dbpass); @mysql_select_db($dbname, $con); $sql=file_get_contents($file_name); //把SQL语句以字符串读入$sql $a=explode(";",$sql); //用explode()函数把‍$sql字符串以“;”分割为数组 foreach($a as $b){ //遍历数组 $c=$b.";"; //分割后是没有“;”的,因为SQL语句以“;”结束,所以在执行SQL前把它加上 @mysql_query($c); //执行SQL语句 //echo $c.'<br />'; }
Run code
Cut to clipboard