18

I was asked to create an export and import mysql database with it's structure and data into a .sql file using php scripting and not phpmyadmin to make the user be able to backup his data?

Does anybody have an idea how to do that??

Thanks in advance

2

3 Answers 3

16

SAFE and WORKING SOLUTION :

EXPORT_TABLES("localhost","user","pass","db_name"); 
// has 5th and 6th optional parameters too.

Latest version is available at github: Export.php + Import.php

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! this is a best code. because other codes work with "exec" or "system" functions.this functions disabled on Linux hosts. but this script worked on Linux hosts.
Awesome... Thanks for sharing this code Mr.tazo...It's working perfectly for me...Thanks.
fantastic and its very useful one.
12

http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx

either

$tableName  = 'mypet';
$backupFile = 'backup/mypet.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);

or

$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip >     $backupFile";
system($command);

2 Comments

anybody knows how to import it to phpmyadmin through a php script
Look at the tutorial. There is a "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";section.
3

Here is a nice class that worked just fine for me. if it says fopen access denied, try to change the folder permissions.

https://github.com/clouddueling/mysqldump-php

Comments

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.