Home > php教程 > php手册 > 一个分析执行SQL文件来做到数据库上传的小例子。

一个分析执行SQL文件来做到数据库上传的小例子。

WBOY
Release: 2016-06-21 09:09:20
Original
1042 people have browsed it

上传|数据|数据库|执行

    我们对数据库进行迁移的时候,用的一般都是mysqldump 或是phpmyadmin
先把某个表的数据倒出到一个 SQL 文件中,然后再用mysql 等工具来执行,以下的例子说明了如何从 SQL 文件中读出并结合成SQL语句,再上传到远和机器上去执行,免去了事先上传的麻烦。供各位参考。


     $hostname="localhost";
    $dbname ="dbname";
    $username = "username";
    $pass = "password";
    mysql_pconnect($hostname, $username, $pass);
    mysql_select_db($dbname);
}
?>


    dump datas






if ($go)
{
echo  "   dumping data please waiting............................
";
echo "your sql file:".$file_name_name."
";
$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 ... ...
"; 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... ...
"; else $success=false;
                    $qu="";
                 }   
           }
  
    }
    if ($success) echo $file_name_name."已顺利执行完毕,请去检查结果";
}
?>

  

请输入数据库SQL文件名:


  


    
  


  


    
  





 



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template