2

Is it possible to export the database with .sql Extension using PHP code?

Example

database name: demodb
DB contains two tables

table one: user

table two: registration

Community
  • 1
  • 1
Anand Somasekhar
  • 586
  • 11
  • 19
  • 1
    All you have to do is build `CREATE` and `INSERT` statements with your PHP code and put into your SQL file. – Rachcha Jan 29 '14 at 07:21

2 Answers2

6

Yes Its possible .

 <?php
        //ENTER THE RELEVANT INFO BELOW
        $mysqlDatabaseName ='xxxxxx';
        $mysqlUserName ='xxxxxxx';
        $mysqlPassword ='myPassword';
        $mysqlHostName ='xxxxxxxx.net';
        $mysqlExportPath ='chooseFilenameForBackup.sql';

        //DO NOT EDIT BELOW THIS LINE
        //Export the database and output the status to the page
        $command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' > ~/' .$mysqlExportPath;
        exec($command);
 ?>
ghost
  • 467
  • 1
  • 6
  • 18
2

use mysqldump command for mySql db:

$command = "/usr/bin/mysqldump -u mysql_username -h mysql_host -p mysql_password database_name> /path_to_export_location/file_name.sql";
exec($command, NULL, NULL);

make a look at documentation in MySQL

ReNiSh AR
  • 2,572
  • 2
  • 24
  • 40