Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php将mysql数据库下载为sql文件_Php_Mysql - Fatal编程技术网

使用php将mysql数据库下载为sql文件

使用php将mysql数据库下载为sql文件,php,mysql,Php,Mysql,在我的网站管理面板中,有一个备份数据库的选项。为此,我使用下面的代码,但我无法使用此代码下载db。有人知道这是怎么发生的吗。 我试图回显$return变量,其输出是正确的,但我无法将该文件作为sql下载 <?php backup_tables('localhost','root','','dbname'); /* backup the db OR just a table */ function backup_tables($host,$user,$pass,$name,$table

在我的网站管理面板中,有一个备份数据库的选项。为此,我使用下面的代码,但我无法使用此代码下载db。有人知道这是怎么发生的吗。 我试图回显
$return
变量,其输出是正确的,但我无法将该文件作为sql下载

<?php

backup_tables('localhost','root','','dbname');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);

  //get all of the tables
  if($tables == '*')
  {
    $tables = array();
    $result = mysql_query('SHOW TABLES');

    while($row = mysql_fetch_row($result))
    {
      $tables[] = $row[0];

    }
  }
  else
  {
    $tables = is_array($tables) ? $tables : explode(',',$tables);
  }

  //cycle through
  foreach($tables as $table)
  {
    $return="";
    $result = mysql_query('SELECT * FROM '.$table);
    $num_fields = mysql_num_fields($result);
    //print_r($num_fields);exit;
    $return.= 'DROP TABLE '.$table.';';
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
    $return.= "\n\n".$row2[1].";\n\n";

    for ($i = 0; $i < $num_fields; $i++) 
    {
      while($row = mysql_fetch_row($result))
      {
        $return.= 'INSERT INTO '.$table.' VALUES(';
        for($j=0; $j<$num_fields; $j++) 
        {
          $row[$j] = addslashes($row[$j]);
         // $row[$j] = preg_replace("\n","\\n",$row[$j]);

          $row[$j] = preg_replace("/(\n){2,}/", "\\n", $row[$j]); 

          if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
          if ($j<($num_fields-1)) { $return.= ','; }
        }
        $return.= ");\n";
      }
    }
    $return.="\n\n\n";

  }

  //save file
  $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
 // print_r($handle);exit;
  fwrite($handle,$return);
  fclose($handle);

}
?>

您可以使用system()调用,只使用mysql备份功能,而不是自己创建所有内容。这是一个小语句,它将在您希望的服务器上创建一个文件。
要下载文件,请在回显内容之前使用header()设置正确的头。

您可以使用system()调用并使用mysql备份功能,而不是自己创建所有内容。这是一个小语句,它将在您希望的服务器上创建一个文件。
要下载文件,请在回显内容之前使用header()设置正确的头。

您正在重新发明轮子!在令人高兴的情况下,这段代码可以为一个表生成一个插入脚本,但在很多特殊情况下,它会失败


使用mysqldump

你正在重新发明轮子!在令人高兴的情况下,这段代码可以为一个表生成一个插入脚本,但在很多特殊情况下,它会失败


使用mysqldump将文件保存在服务器上,而不是将其发送回浏览器进行下载。您需要发送一个包含文件内容的标题以供下载

header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename=db_dump');
echo backup_tables('localhost','root','','dbname'); // change your function to return the data instead saving in a file

但请记住,这不是进行数据库转储的最佳方法。

您将文件保存在服务器上,而不是将其发送回浏览器进行下载。您需要发送一个包含文件内容的标题以供下载

header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename=db_dump');
echo backup_tables('localhost','root','','dbname'); // change your function to return the data instead saving in a file

但是请记住,这不是进行数据库转储的最佳方法。

将整个数据库加载到PHP内存中并不是备份数据库的一种非常明智的方法

假设您没有CLI访问权限来运行mysqldump,也不能在其他地方复制数据库,唯一明智的解决方案是将select语句的输出写入Web服务器缓冲区(定期刷新)

您尝试使用的脚本还包含系统性缺陷。数据写入“return”变量,但函数从未返回此变量。变量在开始处理每个表时被截断

所有这3个问题都将通过替换以下实例来解决:

$return.=

(但别忘了添加一些刷新)


HTH

将整个数据库加载到PHP内存中并不是一种非常明智的数据库备份方式

假设您没有CLI访问权限来运行mysqldump,也不能在其他地方复制数据库,唯一明智的解决方案是将select语句的输出写入Web服务器缓冲区(定期刷新)

您尝试使用的脚本还包含系统性缺陷。数据写入“return”变量,但函数从未返回此变量。变量在开始处理每个表时被截断

所有这3个问题都将通过替换以下实例来解决:

$return.=

(但别忘了添加一些刷新)


HTH

除了前面提到的所有问题(加载到PHP内存和mysqldump的可用性),脚本中还有一个错误,它只返回最后一个表的数据。行
$return=“”需要在foreach循环的外部和前面。比如:

<?php

backup_tables('localhost','root','','dbname');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);
  $return="";

  //get all of the tables
  if($tables == '*')
  {
    $tables = array();
    $result = mysql_query('SHOW TABLES');

    while($row = mysql_fetch_row($result))
    {
      $tables[] = $row[0];

    }
  }
  else
  {
    $tables = is_array($tables) ? $tables : explode(',',$tables);
  }

  //cycle through
  foreach($tables as $table)
  {
    $result = mysql_query('SELECT * FROM '.$table);
    $num_fields = mysql_num_fields($result);
    //print_r($num_fields);exit;
    $return.= 'DROP TABLE '.$table.';';
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
    $return.= "\n\n".$row2[1].";\n\n";

    for ($i = 0; $i < $num_fields; $i++) 
    {
      while($row = mysql_fetch_row($result))
      {
        $return.= 'INSERT INTO '.$table.' VALUES(';
        for($j=0; $j<$num_fields; $j++) 
        {
          $row[$j] = addslashes($row[$j]);
         // $row[$j] = preg_replace("\n","\\n",$row[$j]);

          $row[$j] = preg_replace("/(\n){2,}/", "\\n", $row[$j]); 

          if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
          if ($j<($num_fields-1)) { $return.= ','; }
        }
        $return.= ");\n";
      }
    }
    $return.="\n\n\n";

  }

  //save file
  $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
 // print_r($handle);exit;
  fwrite($handle,$return);
  fclose($handle);

}
?>

除了前面提到的所有问题(加载到PHP内存和mysqldump的可用性),脚本中还有一个错误,只返回最后一个表的数据。行
$return=“”需要在foreach循环的外部和前面。比如:

<?php

backup_tables('localhost','root','','dbname');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{

  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);
  $return="";

  //get all of the tables
  if($tables == '*')
  {
    $tables = array();
    $result = mysql_query('SHOW TABLES');

    while($row = mysql_fetch_row($result))
    {
      $tables[] = $row[0];

    }
  }
  else
  {
    $tables = is_array($tables) ? $tables : explode(',',$tables);
  }

  //cycle through
  foreach($tables as $table)
  {
    $result = mysql_query('SELECT * FROM '.$table);
    $num_fields = mysql_num_fields($result);
    //print_r($num_fields);exit;
    $return.= 'DROP TABLE '.$table.';';
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
    $return.= "\n\n".$row2[1].";\n\n";

    for ($i = 0; $i < $num_fields; $i++) 
    {
      while($row = mysql_fetch_row($result))
      {
        $return.= 'INSERT INTO '.$table.' VALUES(';
        for($j=0; $j<$num_fields; $j++) 
        {
          $row[$j] = addslashes($row[$j]);
         // $row[$j] = preg_replace("\n","\\n",$row[$j]);

          $row[$j] = preg_replace("/(\n){2,}/", "\\n", $row[$j]); 

          if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
          if ($j<($num_fields-1)) { $return.= ','; }
        }
        $return.= ");\n";
      }
    }
    $return.="\n\n\n";

  }

  //save file
  $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
 // print_r($handle);exit;
  fwrite($handle,$return);
  fclose($handle);

}
?>