Hi guys! Is there anyone can help me to do import and export database from my sql? i don't have any idea how to do it. I don't want to do it manually which is using phpmyadmin. So, i want to make it easy only by click the export button. This is what i've already got

ob_start();

$username = "root"; 
$password = "1234"; 
$hostname = "localhost"; 
$dbname   = "annualreport1";

// if mysqldump is on the system path you do not need to specify the full path
// simply use "mysqldump --add-drop-table ..." in this case
$command = "C:\\AppServ\\MySQL\\bin\\mysqldump --add-drop-table --host=$hostname
    --user=$username ";
if ($password) 
        $command.= "--password=". $password ." "; 
$command.= $dbname;
system($command);

$dump = ob_get_contents(); 
ob_end_clean();

// send dump file to the output
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" .date("Y-m-d_H-i-s").".sql"));
flush();
echo $dump;
exit();

but this error keep showed up

Warning: Cannot modify header information - headers already sent in C:\AppServ\www\FSAS\export.php on line 22

Warning: Cannot modify header information - headers already sent in C:\AppServ\www\FSAS\export.php on line 23

Warning: Cannot modify header information - headers already sent in C:\AppServ\www\FSAS\export.php on line 24
Usage: mysqldump [OPTIONS] database [tables] OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] OR mysqldump [OPTIONS] --all-databases [OPTIONS] For more options, use mysqldump --help

Your help are much appreciated.

Recommended Answers

All 3 Replies

Member Avatar for LastMitch

There should be only 1 header that is connected to the db and send.

You have 3.

Try taking away this:

header('Content-Disposition: attachment; filename='.basename($dbname . "_" .date("Y-m-d_H-i-s").".sql"));

What error do you get?

So you want to Import and export database from mysql without using phpmyadmin:

Read this and try the example:

http://zetcode.com/databases/mysqltutorial/exportimport/

Thanks LastMitch!

I've edit the code so it become like this and it is functioning well. By any chance,is there any one know the code so that the export sql database can be save wherever the user like.Such as "Save As.." function. Thank you

Here is the code that i have for now

<?php
$db_host = 'localhost';                  // database server
$db_user = 'root';                    // database user name
$db_pwd = '1234';             // database password
$db_schema = 'AnnualReport';         // name of database to be exported
$today=date("Y-m-d");
// Command to take backup of database
    exec(sprintf(
    'c:\AppServ\MySQL\bin\mysqldump.exe --opt -h%s -u%s -p%s %s > /AppServ/data/backup_filename_%s.sql',
    $db_host,
    $db_user,
    $db_pwd,
    $db_schema,
    $today
    ));

    echo "Your Database have been exported";
    exit;

?>
Member Avatar for LastMitch

@eyeda

I've edit the code so it become like this and it is functioning well.
By any chance,is there any one know the code so that the export sql database can be save wherever the user like.Such as "Save As.." function. Thank you

I'm a bit confused what do you mean export data and save wherever the user want?

Usually, it's only the person who have access to the db can do that. Which is why you have this command in the sprintf() function.

Can you explain more clearly what you are trying to do?

Regarding about what file do you want to save as?

You can export it as a txt, cvs or sql by using a command line.

The link I already provide from my previous thread:

http://zetcode.com/databases/mysqltutorial/exportimport/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.