微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php将mysql数据库整库导出生成sql文件

<?PHP

ini_set("max_execution_time","180");//避免数据量过大,导出不全的情况出现。

$host="localhost";//数据库地址

$dbname="db";//这里配置数据库

$username="root";//用户名

$passw="root";//这里配置密码

$filename=date("Y-m-d_H-i-s")."-".$dbname.".sql";

    header("Content-disposition:filename=".$filename);//所保存的文件

    header("Content-type:application/octetstream");

    header("Pragma:no-cache");

    header("Expires:0");

//备份数据

    $i   =   0;

    $crlf="rn";

    global     $dbconn;

    $dbconn   =   MysqL_connect($host,$username,$passw);//数据库主机,用户名密码

    $db   =   MysqL_select_db($dbname,$dbconn);

    MysqL_query("SET NAMES 'utf8'");

    //$tables =MysqL_list_tables($dbname,$dbconn);

    $tables =MysqL_query("SHOW TABLES FROM $dbname");

    $num_tables   =   @MysqL_numrows($tables);

    print   "-- filename=".$filename;

    while($i   <   $num_tables)

    {

      $table=MysqL_tablename($tables,$i);

      print   $crlf;

      echo   get_table_structure($dbname,   $table,   $crlf).";$crlf$crlf";

      //echo   get_table_def($dbname,   $crlf).";$crlf$crlf";

      echo   get_table_content($dbname,   $crlf);

      $i++;

   }

function get_table_structure($db,$table,$crlf){

    global $drop;

    $schema_create   =   "";

    if(!empty($drop)){ $schema_create   .=   "DROP TABLE IF EXISTS `$table`;$crlf";}

    $result   =MysqL_query("SHOW CREATE TABLE $table");

    $row=MysqL_fetch_array($result);

    $schema_create   .= $crlf."-- ".$row[0].$crlf;

    $schema_create   .= $row[1].$crlf;

    Return $schema_create;

}

//获得表内容

function   get_table_content($db,   $crlf){

          $schema_create   =   "";

          $temp   =   "";

          $result   =   MysqL_query("SELECT * FROM $table");

          $i   =   0;

          while($row   =   MysqL_fetch_row($result))

          {

                  $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   =   preg_replace("/,$/","",$schema_insert);

                  $schema_insert   .=   ");$crlf";

                  $temp   =   $temp.$schema_insert   ;

                  $i++;

          }

          return   $temp;

}

?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐