Last active
September 12, 2024 22:27
-
-
Save famoser/c94968775339d4d57770e73c4b973a7b to your computer and use it in GitHub Desktop.
import / export php mysql database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Import a .sql file in a MySQL database | |
* | |
* Usage: | |
* | |
* 1. edit the configuration parameters here below and save | |
* 2. upload to the proper folder - usually /public_html - on your server | |
* 3. open a browser and execute with with an URL similar to: | |
* http://example.com/dbexport.php | |
* | |
* Changing example.com with the name of your site | |
* | |
* (c) 2013 http://grandolini.com | |
/** | |
* Full path to the file you want to export | |
* Note: The script must be able to write the file in the given folder | |
* | |
* Examples: | |
* | |
* to write the file in the main public folder - where usually your site files are located: | |
* $filename = '~/public_html/mydata.sql'; | |
* | |
* to write the file in your main folder - where nobody will be able to see it with a browser: | |
* $filename = '~/mydata.sql'; | |
* | |
* to write the file in the same folder where you install this script: | |
* $filename = 'mydata.sql'; | |
*/ | |
$filename = 'mydata.sql'; | |
/** | |
* MySQL connection configuration | |
*/ | |
$database = 'Enter the name of the database where you want to import your data'; | |
$user = 'Enter the MySQL user to access the database'; | |
$password = 'Enter the MySQL password to access the database'; | |
/** | |
* usually it's ok to leave the MySQL host as 'localhost' | |
* if your hosting provider instructed you differently, edit the next one as needed | |
*/ | |
$host = 'localhost'; | |
/** | |
* DO NOT EDIT BELOW THIS LINE | |
*/ | |
$fp = @fopen( $filename, 'w+' ); | |
if( !$fp ) { | |
echo 'Impossible to create <b>'. $filename .'</b>, please manually create one and assign it full write privileges: <b>777</b>'; | |
exit; | |
} | |
fclose( $fp ); | |
$command = 'mysqldump --opt -h '. $host .' -u '. $user .' -p'. $password .' '. $database .' > '. $filename; | |
exec( $command, $output = array(), $worked ); | |
switch( $worked ) { | |
case 0: | |
echo 'Database <b>'. $database .'</b> successfully exported to <b>'. $filename .'</b>'; | |
break; | |
case 1: | |
echo 'There was a warning during the export of <b>'. $database .'</b> to <b>'. $filename .'</b>'; | |
break; | |
case 2: | |
echo 'There was an error during import.' | |
. 'Please make sure the import file is saved in the same folder as this script and check your values:' | |
. '<br/><br/><table>' | |
. '<tr><td>MySQL Database Name:</td><td><b>'. $database .'</b></td></tr>' | |
. '<tr><td>MySQL User Name:</td><td><b>'. $user .'</b></td></tr>' | |
. '<tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr>' | |
. '<tr><td>MySQL Host Name:</td><td><b>'. $host .'</b></td></tr>' | |
. '<tr><td>MySQL Import Filename:</td><td><b>'. $filename .'</b></td>' | |
. '</tr></table>' | |
; | |
break; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Import a .sql file in a MySQL database | |
* | |
* Usage: | |
* | |
* 1. edit the configuration parameters here below and save | |
* 2. upload to the proper folder - usually /public_html - on your server | |
* 3. open a browser and execute with with an URL similar to: | |
* http://example.com/dbimport.php | |
* | |
* Changing example.com with the name of your site | |
* | |
* (c) 2013 http://grandolini.com | |
*/ | |
/** | |
* Full path to the file you want to import | |
* It must be a valid sql file, like the one exported by phpMyAdmin or the dbexport.php | |
* file you found in the same zip as this script | |
* | |
* Examples: | |
* $filename = '~/public_html/mydata.sql'; | |
* $filename = '~/mydata.sql'; | |
* $filename = 'mydata.sql'; | |
*/ | |
$filename = 'mydata.sql'; | |
/** | |
* MySQL connection configuration | |
*/ | |
$database = 'Enter the name of the database where you want to import your data'; | |
$user = 'Enter the MySQL user to access the database'; | |
$password = 'Enter the MySQL password to access the database'; | |
/** | |
* usually it's ok to leave the MySQL host as 'localhost' | |
* if your hosting provider instructed you differently, edit the next one as needed | |
*/ | |
$host = 'localhost'; | |
/** | |
* DO NOT EDIT BELOW THIS LINE | |
*/ | |
$command = 'mysql -h '. $host .' -u '. $user .' -p'. $password .' '. $database .' < '. $filename; | |
exec( $command, $output = array(), $worked ); | |
switch( $worked ) { | |
case 0: | |
echo 'Import file <b>'. $filename .'</b> successfully imported to database <b>'. $database .'</b>'; | |
break; | |
case 1: | |
echo 'There was an error during import.' | |
. 'Please make sure the import file is saved in the same folder as this script and check your values:' | |
. '<br/><br/><table>' | |
. '<tr><td>MySQL Database Name:</td><td><b>'. $database .'</b></td></tr>' | |
. '<tr><td>MySQL User Name:</td><td><b>'. $user .'</b></td></tr>' | |
. '<tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr>' | |
. '<tr><td>MySQL Host Name:</td><td><b>'. $host .'</b></td></tr>' | |
. '<tr><td>MySQL Import Filename:</td><td><b>'. $filename .'</b></td>' | |
. '</tr></table>' | |
; | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The close($fp); on line 55 of dbexport.php will throw an error as being undefined. It should probably be fclose($fp);.