tags:

views:

59

answers:

5

How to backup the mysql database and download it as a .sql file by using PHP Codes

A: 

Do you have phpmyadmin? If so you can export it from there by clicking "Export" at the top (on the selected table/db).

Alex
I want to download the file using PHP script, from my Admin Panel
mrNepal
+3  A: 
mysqldump -u username -p password database > file

Alternatively, phpMyAdmin can do this too with the Export tool.

Delan Azabani
Much faster than phpMyAdmin, but requires shell access. On the other hand, PMA will work just fine if you only have web access.
Piskvor
I want to download the file using PHP script, from my admin panel
mrNepal
+1  A: 

Use phpmyadmin

Edit:

You can use shell_exec to execute this command

mysqldump -u username -p password database > file

This will generate a dump file,and then redirect user to this generated file.

org.life.java
I want to download the file using PHP script, from my Admin Panel
mrNepal
A: 

If you have phpMyAdmin you can do it at the Export menu.

If you look for a command-line tool take a look at mysqldump.

fabrik
+1  A: 

A very simple solution would be something like (first example): http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx

Naturally this will only make a Data dump of the table.

What you could do is use this code:

http://snipplr.com/view/173/mysql-dump/

What this code does is actually gets a description of the table (i.e its structure), creates all the tables and pushes data. pretty much like any other tool does.

Then its just a matter of saving it from string to a file (file_put_contents() for instance or something similar, depending on your preference and need)

AlexejK
You may, naturally, also just output the contents of the string and force download of it..
AlexejK