0

i want to backup table by table from databse mysql with php using mysqldump,,,,

my table show on list using checkbox.

if list of table checked all then my all table backup

if list of table one,two,three,etc checked the my table checked backup

this is my code:

<?php
    include "config/conn.php";
    echo "<h1>Database name: ".$db."</h1></br>";
    echo "list of Tabel:";
    $query = "SHOW TABLES";
    $hasil = mysql_query($query);

    echo "<form method='post' action='db/backupAction.php'>";
    echo "<table>";
    while ($data = mysql_fetch_row($hasil))
    {
       echo "<tr><td><input type='checkbox' name='tabel[]' value='".$data[0]."'></td><td>".$data[0]."</td></tr>";
    }
    echo "</table><br>";
    echo "<input type='submit' name='submit' value='Backup Data' class='btn'>";
    echo "</form>";

?>

and this is my file of backupAction.php

<?php
include "config/conn.php";

$tabel = $_POST['tabel'];


$listTabel = "";
foreach($tabel as $namatabel)
{
  $listTabel = $namatabel ;
}

$command = "C:\xampp\Mysql\bin\mysqldump --user".$user." --password=".$pass." ".$db." ".$listTabel."\>".$db.".sql";

exec($command);


header("Content-Disposition: attachment; filename=".$db.".sql");
header("Content-type: application/download");

//$fp  = fopen ($db.".sql", 'r');
//$content = fread($fp, filesize($db.".sql"));
//fclose($fp);

//echo $content;

//exit;
?>

my result file .sql is null

why???

3
  • Can you echo out $command? Does it work when you run it from the command line? Commented May 28, 2013 at 13:31
  • "\>" seems invalid in your $command please echo it. Commented May 28, 2013 at 13:35
  • my result echo is 'array', if my input name i am change name='tabel' my result is table name, but result is same null or blank Commented May 28, 2013 at 13:55

1 Answer 1

1

There are two issues with your above code. Firstly, would should make sure that you separate the user in your mysqldump command.

  mysqldump --user=".$user."

In the for each loop that you are doing to create the listTabel string, you are not adding spaces at the end. Additionally, there is no need to use a look to concatenate the strings, you can use PHP's built in implode function.

Secondly, you should add the --tables argument before you start echoing out all of the table names that you will be backing up.

Finally, in PHP "" is a special escape character. You either need to use two, or wrap the commands in single quotes. You do not need to escape the ">" character.

Essentially, the command will look like this:

$listTabel = '';
if (is_array($tabel))
    $listTabel = implode(" ", $tabel);

$executable = 'C:\xampp\Mysql\bin\mysqldump ';
$command = $executable;
if (isset($user) && $user != '')
    $command .= '--user='.$user.' ';
if (isset($pass) && $pass != '')
    $command .= '--password='.$pass.' ';

$command .= $db

if (isset($listTabel) && $listTabel != '')
    $command .=' --tables'.$listTabel;

$command .= ' > '.$db.'.sql';
Sign up to request clarification or add additional context in comments.

6 Comments

same, my result is null or blank
Please let us know what echo $command; exit; outputs just after $command = ...
i change using implode my result is implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in.......
$command i am echo then result is C:\xampp\Mysql\bin\mysqldump --user=root --password= myDb --tables > myDb.sql
OK, that looks like then that your $tabel is not correctly populated. Please do a print_r/var_dump on $tabel just under $tabel = $_POST['tabel']; and let us know what it outputs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.