日期:2010-12-29  浏览次数:20597 次


    我们对数据库进行迁移的时候,用的一般都是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;
  &nbs