• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)_php实例

php 搞代码 3年前 (2022-01-25) 18次浏览 已收录 0个评论

用php代码实现数据库备份可)本文来(源gaodai#ma#com搞@@代~&码*网2

搞代gaodaima码

以使网站的管理变得非常便捷,我们可以直接进后台操作就能完成数据库的备份。

关键技术:

1. 首先要得到该数据库中有哪些表,所用函数 mysql_list_tables(),然后可以将获取的所有表名存到一个数组。
2. show create table 表名 可以获取表结构。
3. select * from 表名 取出所有记录,用循环拼接成 insert into… 语句。

实现代码:

<?php<br><br> header("Content-type:text/html;charset=utf-8");<br><br> //配置信息<BR> $cfg_dbhost = 'localhost';<BR> $cfg_dbname = 'ftdm';<BR> $cfg_dbuser = 'root';<BR> $cfg_dbpwd = 'root';<BR> $cfg_db_language = 'utf8';<BR> $to_file_name = "ftdm.sql";<BR> // END 配置</P><P> //链接数据库<BR> $link = mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);<BR> mysql_select_db($cfg_dbname);<BR> //选择编码<BR> mysql_query("set names ".$cfg_db_language);<BR> //数据库中有哪些表<BR> $tables = mysql_list_tables($cfg_dbname);<BR> //将这些表记录到一个数组<BR> $tabList = array();<BR> while($row = mysql_fetch_row($tables)){<BR>  $tabList[] = $row[0];<BR> }<br><br> echo "运行中,请耐心等待...<br />";<BR> $info = "-- ----------------------------\r\n";<BR> $info .= "-- 日期:".date("Y-m-d H:i:s",time())."\r\n";<BR> $info .= "-- 仅用于测试和学习,本程序不适合处理超大量数据\r\n";<BR> $info .= "-- ----------------------------\r\n\r\n";<BR> file_put_contents($to_file_name,$info,FILE_APPEND);</P><P> //将每个表的表结构导出到文件<BR> foreach($tabList as $val){<BR>  $sql = "show create table ".$val;<BR>  $res = mysql_query($sql,$link);<BR>  $row = mysql_fetch_array($res);<BR>  $info = "-- ----------------------------\r\n";<BR>  $info .= "-- Table structure for `".$val."`\r\n";<BR>  $info .= "-- ----------------------------\r\n";<BR>  $info .= "DROP TABLE IF EXISTS `".$val."`;\r\n";<BR>  $sqlStr = $info.$row[1].";\r\n\r\n";<BR>  //追加到文件<BR>  file_put_contents($to_file_name,$sqlStr,FILE_APPEND);<BR>  //释放资源<BR>  mysql_free_result($res);<BR> }</P><P> //将每个表的数据导出到文件<BR> foreach($tabList as $val){<BR>  $sql = "select * from ".$val;<BR>  $res = mysql_query($sql,$link);<BR>  //如果表中没有数据,则继续下一张表<BR>  if(mysql_num_rows($res)<1) continue;<BR>  //<BR>  $info = "-- ----------------------------\r\n";<BR>  $info .= "-- Records for `".$val."`\r\n";<BR>  $info .= "-- ----------------------------\r\n";<BR>  file_put_contents($to_file_name,$info,FILE_APPEND);<BR>  //读取数据<BR>  while($row = mysql_fetch_row($res)){<BR>   $sqlStr = "INSERT INTO `".$val."` VALUES (";<BR>   foreach($row as $zd){<BR>    $sqlStr .= "'".$zd."', ";<BR>   }<BR>   //去掉最后一个逗号和空格<BR>   $sqlStr = substr($sqlStr,0,strlen($sqlStr)-2);<BR>   $sqlStr .= ");\r\n";<BR>   file_put_contents($to_file_name,$sqlStr,FILE_APPEND);<BR>  }<BR>  //释放资源<BR>  mysql_free_result($res);<BR>  file_put_contents($to_file_name,"\r\n",FILE_APPEND);<BR> }<br><br> echo "OK!";<br><br>?>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP实现把MySQL数据库导出为.sql文件实例(仿PHPMyadmin导出功能)_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址