mysql to single byte_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...

# Answer 1

4d350fd91e33782268f371d7edaa8a76.png

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值