Monday, 18 November 2013

How to export DB in PHP


If your database is too large to import using phpMyAdmin or you do not have a package that allows SSH access, you can perform import/export actions using the PHP script. The process is the same whether you are importing or exporting. The only difference will be in the actual PHP script.
 
1.Create index.php :-
<a href="db_back.php">Export</a>
2.Create db_back.php :-
<?php
ob_start();
$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "";
$command = "mysqldump.exe -p -u root demo";
if ($password)
$command.= "--password=". $password ." ";
$command.= $dbname;
system($command);
$dump = ob_get_contents();
ob_end_clean();
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();
?>