资讯   |   开发   |   选机中心   |   产品大全 | IBM | 惠普 | 联想 | 戴尔 | 苹果 | 神舟
更多: | 华硕 | 明基 | 方正 | 紫光 | TCL | 夏新 | 联宝 | 宏碁 | 七喜 | 长城 | 清华同方 | 海尔 | 三星 | 东芝 | 索尼 | 富士通 | LG | 技术 | ddnoon
当前位置:笔记本 > 软件开发 >
Advertisement
文章正文

如何实现phpmyadmin里的导出sql

类型:转载   责任编辑:asp.net   日期:2007/05/23


热门软件下载:


   

如何写程序能实现和phpmyadmin里导出sql一样的效果。  
  谁能给个思路或例子。

网友回答:

发表者:subzero

查看phpmyadmin根目录下  
  server_export.php和export.php

发表者:jxflll

这是我以前写过的一个把数据导出成*.sql的一段代码。你看看。希望会对你有帮助。  
  <?php  
  //连接数据库  
  mysql_connect("localhost","root","root");  
  //数据库名  
  mysql_select_db("test");  
   
  @set_time_limit(600);  
  $crlf="\n";  
  $db=test;  
   
   
  $CurD=date("Ymd");  
  $SaveFile=$db."_".$CurD;  
  //   include("lib.inc.php");  
  header("Content-disposition:   filename=$SaveFile.sql");  
  header("Content-type:   application/octetstream");  
  header("Pragma:   no-cache");  
  header("Expires:   0");  
   
  //   doing   some   DOS-CRLF   magic...  
  $client   =   getenv("HTTP_USER_AGENT");  
  if(ereg([^(]*\((.*)\)[^)]*,$client,$regs))    
  {  
  $os   =   $regs[1];  
  //   this   looks   better   under   WinX  
  if   (eregi("Win",$os))    
  $crlf="\r\n";  
  }  
   
  function   my_handler($sql_insert)  
  {  
          global   $crlf,   $asfile;  
           
          if(empty($asfile))    
          {  
                  echo   htmlspecialchars("$sql_insert;$crlf");  
          }  
          else  
          {  
                  echo   "$sql_insert;$crlf";  
          }  
  }  
   
  $tables   =   mysql_list_tables($db);  
   
  $num_tables   =   @mysql_numrows($tables);  
  if($num_tables   ==   0)  
  {  
          echo   $strNoTablesFound;  
  }  
  else  
  {  
          $i   =   0;  
          print   "#   MySQL-Dump$crlf";  
          print   "#   http://3q.com/$crlf";  
          print   "#$crlf";  
          print   "#   $strHost:   "   .   $host;  
          if(!empty($port))    
          {  
                  print   ":"   .   $port;  
          }  
          print   "   $strDatabase:   $db$crlf";  
   
          while($i   <   $num_tables)  
          {    
                  $table   =   mysql_tablename($tables,   $i);  
   
                  print   $crlf;  
                  print   "#   --------------------------------------------------------$crlf";  
                  print   "#$crlf";  
                  print   "#   $strTableStructure   $table$crlf";  
                  print   "#$crlf";  
                  print   $crlf;  
   
                  echo   get_table_def($db,   $table,   $crlf).";$crlf$crlf";  
                   
                  if($what   ==   "data")  
                  {  
                          print   "#$crlf";  
                          print   "#   $strDumpingData   $table$crlf";  
                          print   "#$crlf";  
                          print   $crlf;  
                   
                          get_table_content($db,   $table,   "my_handler");  
                  }  
                  $i++;  
          }  
   
  }  
   
  function   get_table_def($db,   $table,   $crlf)  
  {  
          global   $drop;  
   
          $schema_create   =   "";  
          if(!empty($drop))  
                  $schema_create   .=   "DROP   TABLE   IF   EXISTS   $table;$crlf";  
   
          $schema_create   .=   "CREATE   TABLE   $table   ($crlf";  
   
          $result   =   mysql_db_query($db,   "SHOW   FIELDS   FROM   $table")   or   mysql_die();  
          while($row   =   mysql_fetch_array($result))  
          {  
                  $schema_create   .=   "       $row[Field]   $row[Type]";  
   
                  if(isset($row["Default"])   &&   (!empty($row["Default"])   ||   $row["Default"]   ==   "0"))  
                          $schema_create   .=   "   DEFAULT   $row[Default]";  
                  if($row["Null"]   !=   "YES")  
                          $schema_create   .=   "   NOT   NULL";  
                  if($row["Extra"]   !=   "")  
                          $schema_create   .=   "   $row[Extra]";  
                  $schema_create   .=   ",$crlf";  
          }  
          $schema_create   =   ereg_replace(",".$crlf."$",   "",   $schema_create);  
          $result   =   mysql_db_query($db,   "SHOW   KEYS   FROM   $table")   or   mysql_die();  
          while($row   =   mysql_fetch_array($result))  
          {  
                  $kname=$row[Key_name];  
                  if(($kname   !=   "PRIMARY")   &&   ($row[Non_unique]   ==   0))  
                          $kname="UNIQUE|$kname";  
                    if(!isset($index[$kname]))  
                            $index[$kname]   =   array();  
                    $index[$kname][]   =   $row[Column_name];  
          }  
   
          while(list($x,   $columns)   =   @each($index))  
          {  
                    $schema_create   .=   ",$crlf";  
                    if($x   ==   "PRIMARY")  
                            $schema_create   .=   "       PRIMARY   KEY   ("   .   implode($columns,   ",   ")   .   ")";  
                    elseif   (substr($x,0,6)   ==   "UNIQUE")  
                          $schema_create   .=   "       UNIQUE   ".substr($x,7)."   ("   .   implode($columns,   ",   ")   .   ")";  
                    else  
                          $schema_create   .=   "       KEY   $x   ("   .   implode($columns,   ",   ")   .   ")";  
          }  
   
          $schema_create   .=   "$crlf)";  
          return   (stripslashes($schema_create));  
  }  
   
  //   Get   the   content   of   $table   as   a   series   of   INSERT   statements.  
  //   After   every   row,   a   custom   callback   function   $handler   gets   called.  
  //   $handler   must   accept   one   parameter   ($sql_insert);  
  function   get_table_content($db,   $table,   $handler)  
  {  
          $result   =   mysql_db_query($db,   "SELECT   *   FROM   $table")   or   mysql_die();  
          $i   =   0;  
          while($row   =   mysql_fetch_row($result))  
          {  
                  set_time_limit(60);   //   HaRa  
                  $table_list   =   "(";  
   
                  for($j=0;   $j<mysql_num_fields($result);$j++)  
                          $table_list   .=   mysql_field_name($result,$j).",   ";  
   
                  $table_list   =   substr($table_list,0,-2);  
                  $table_list   .=   ")";  
   
                  if(isset($GLOBALS["showcolumns"]))  
                          $schema_insert   =   "INSERT   INTO   $table   $table_list   VALUES   (";  
                  else  
                          $schema_insert   =   "INSERT   INTO   $table   VALUES   (";  
   
                  for($j=0;   $j<mysql_num_fields($result);$j++)  
                  {  
                          if(!isset($row[$j]))  
                                  $schema_insert   .=   "   NULL,";  
                          elseif($row[$j]   !=   "")  
                                  $schema_insert   .=   "   ".addslashes($row[$j]).",";  
                          else  
                                  $schema_insert   .=   "   ,";  
                  }  
                  $schema_insert   =   ereg_replace(",$",   "",   $schema_insert);  
                  $schema_insert   .=   ")";  
                  $handler(trim($schema_insert));  
                  $i++;  
          }  
          return   (true);  
  }  
   
  function   count_records   ($db,$table)  
  {  
          $result   =   mysql_db_query($db,   "select   count(*)   as   num   from   $table");  
          $num   =   mysql_result($result,0,"num");  
          echo   $num;  
  }  
   
  function   get_table_csv($db,   $table,   $sep,   $handler)  
  {  
          $result   =   mysql_db_query($db,   "SELECT   *   FROM   $table")   or   mysql_die();  
          $i   =   0;  
          while($row   =   mysql_fetch_row($result))  
          {  
                  set_time_limit(60);   //   HaRa  
                  $schema_insert   =   "";  
                  for($j=0;   $j<mysql_num_fields($result);$j++)  
                  {  
                          if(!isset($row[$j]))  
                                  $schema_insert   .=   "NULL".$sep;  
                          elseif   ($row[$j]   !=   "")  
                                  $schema_insert   .=   "$row[$j]".$sep;  
                          else  
                                  $schema_insert   .=   "".$sep;  
                  }  
                  $schema_insert   =   str_replace($sep."$",   "",   $schema_insert);  
                  $handler(trim($schema_insert));  
                  $i++;  
          }  
          return   (true);  
  }  
   
  if(empty($asfile))  
  {  
          print   "</pre></div>\n";  
          //include   ("footer.inc.php");  
  }  
  ?>  
 


 

 
热门推荐笔记本: IBM笔记本
相关文章:
webmaster:popbb@126.com   最佳浏览:1024X768 MSIE
©2007 popbb.net All Rights Reserved