我们对数据库进行迁移的时候,用的一般都是mysqldump 或是phpmyadmin 先把某个表的数据倒出到一个 SQL 文件中,然后再用mysql 等工具来执行,以下的例子说明了如何从 SQL 文件中读出并结合成SQL语句,再上传到远和机器上去执行,免去了事先上传的麻烦。供各位参考。 <html> <?php $hostname="localhost"; $dbname ="dbname"; $username = "username"; $pass = "password"; mysql_pconnect($hostname, $username, $pass); mysql_select_db($dbname); } ?> <head> <title>dump datas</title> </head> <body> <form name="form1" > </form> <? if ($go) { echo " dumping data please waiting............................<BR>"; echo "your sql file:".$file_name_name."<br>"; $success=true; $fp=fopen($file_name,"r"); $qu =''; while ($str_len =fgets($fp,256)) { $len=strlen($strlen); if (substr($str_len,0,1)!="#") { $qu.=$str_len; if (substr($str_len,0,4)=="DROP") { //echo $str_len; $qu=substr($qu,0,strlen($qu)-2); //echo $qu." afdf"; $re=mysql_db_query($dbname,$qu) or die(mysql_error().$qu); if ($re) echo "drop ing table ... ...<br>"; else $success=false; $qu=""; } $sub_b=strlen($str_len)-4; if (substr($str_len,strlen($str_len)-4,2)==");") { $qu=substr($qu,0,strlen($qu)-2); $re=mysql_db_query($dbname,$qu) or die(mysql_error().$qu); if ($re) echo "inserting... ...<BR>"; else $success=false; $qu=""; } } } if ($success) echo $file_name_name."已顺利执行完毕,请去检查结果"; } ?> <form method="post" action="dumpcomm.php3" enctype="multipart/form-data"> <p>请输入数据库SQL文件名:</p> <p> <input type="file" name="file_name"> </p> <p> <input type="submit" name="go" value=" 运 行 "> </p> </form> </body> </html>