简体   繁体   中英

mysqldump with php to export a single table

I want to export one table from my database into a.sql file using mysqldump .

My code almost works:

$problem = ' --databases '.$db_name;

$cmd  = 'mysqldump';
$cmd .= ' --host='.$dbhost;
$cmd .= ' --user='.$dbuser;
$cmd .= ' --password='.$dbpass;
$cmd .= $problem;
$cmd .= ' > '.$filename;

shell_exec( $cmd.' '.$filename)

This code works fantastic, giving me a dump of the entire database into the file I've specified.

However: Now I only want to get one table dumped.

Let's modify the variable $problem :

option 1

$problem  = ' --databases '.$db_name.' '.$table_name;

This still dumps the entire DB

option 2

$problem  = ' --databases '.$db_name;
$problem .= ' --tables '.$table_name;

option 3

$problem = ' --tables '.$table_name;

option 4

$problem = ' --tables '.$table_name.' '.$db_name;

option 5

$problem = ' '.$db_name.' '.$table_name;

Options 2 through 5 result in only some kind of header-like information being output into the file:

-- MySQL dump 10.13  Distrib 5.5.58, for debian-linux-gnu (x86_64)
--
-- Host: mysql.mydomain.com    Database: [$db_name or $table_name]
-- ------------------------------------------------------
-- Server version   5.7.29-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

Reading the Documentation hasn't given me any more insight...

The error was this:

shell_exec( $cmd.' '.$filename);

has to be changed to

shell_exec( $cmd);

$filename had already been included into $cmd .

Once fixed, all the code works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM